Question

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?

Was it helpful?

Solution

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!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top