문제

Here i have a config file and i am reading that config elements in c#. What i need is to read all the elements based on the host <Brand Host="localhost:64995"> .For example if the host is localhost:64995 i need the nodes inside it like <add Name="aaa" BrandId="13" />

Here is my config

<SingleSignOn>
        <Brands>
          <Brand Host="localhost:64995">
          <add Name="aaa" BrandId="1" />
          </Brand>
          <Brand Host="aaaaa">
            <add Name="bbbb" BrandId="2"  />
          </Brand>
        </Brands>
      </SingleSignOn>

and my code

string host = GetHostUrl();
   List<ConfigurationContract> branditems = null;
   XmlDocument xdoc = new XmlDocument();
   xdoc.Load(HttpContext.Current.Server.MapPath("~/") + "SSO.config");
   XmlNode xnodes = xdoc.SelectSingleNode("configuration/SingleSignOn/Brands");
   foreach (XmlNode xnn in xnodes.ChildNodes)
   {

   }

and here i will pass the host value from this string host = GetHostUrl(); and if the host value matches the element in the config it should read and get that element.

Any suggestion?

도움이 되었습니까?

해결책

Inside your foreach, you can use

if (xnn.Attributes["Host"].Value == host)
{
  foreach (XmlNode i in xnn.ChildNodes) // i is the child node inside <Brand Host="localhost:64995"></Brand>
  {
    if (i.Attributes["Name"].Value == "aaa") // or even i.Attributes["BrandId"].Value == "1"
    {
       // Do your stuff
    }
  }
 }

Hope it helps!

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top