Reading a particular config element using c#?

本小妞迷上赌 提交于 2020-01-24 21:28:08

问题


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?


回答1:


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!



来源:https://stackoverflow.com/questions/21399599/reading-a-particular-config-element-using-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!