使用c#读取特定的配置元素? [英] Reading a particular config element using c#?
问题描述
这里我有一个配置文件,我正在读取配置元素在c#。我需要的是读取所有的元素基于主机< Brand Host =localhost:64995>
。例如如果主机是localhost:64995我需要其中的节点像< add Name =aaaBrandId =13/>
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" />
是我的配置
<SingleSignOn>
<Brands>
<Brand Host="localhost:64995">
<add Name="aaa" BrandId="1" />
</Brand>
<Brand Host="aaaaa">
<add Name="bbbb" BrandId="2" />
</Brand>
</Brands>
</SingleSignOn>
和我的代码
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)
{
}
b $ b
,这里我将从这个 string host = GetHostUrl();
传递主机值,如果主机值匹配配置中的元素,
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.
任何建议?
推荐答案
foreach
,您可以使用
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
}
}
}
$ b b
希望它有帮助!
Hope it helps!
这篇关于使用c#读取特定的配置元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!