Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Wednesday, April 23, 2014 12:23 PM
Hello,
Maybe it's just basics but how I can select node using attribute value. This is my XML loaded to XmlDocument:
<?xml version="1.0" encoding="utf-8"?>
<ConfigTree>
<root label="C:\Windows\Microsoft.NET\Framework64\v2.0.50727\Config\web.config" inherents="false">
<node label="C:" inherents="false">
<node label="inetpub" inherents="false">
<node label="BlogEngine.NET" inherents="false">
<node label="Web.Config" inherents="false" />
<node label="Account" inherents="false">
<node label="Web.Config" inherents="false" />
</node>
<node label="admin" inherents="false">
<node label="Web.Config" inherents="false" />
<node label="Comments" inherents="false">
<node label="Web.Config" inherents="false" />
</node>
<node label="Pages" inherents="false">
<node label="Web.Config" inherents="false" />
</node>
<node label="Users" inherents="false">
<node label="Web.Config" inherents="false" />
</node>
</node>
</node>
</node>
</node>
</root>
</ConfigTree>
Lets say I want to select the underlined node. I am aware of the full address of the file:
string path = "c:\\inetpub\\BlogEngine.NET\\admin\\Comments\\Web.config";
how I can select XmlNode represented by the path variable?
All replies (1)
Wednesday, April 23, 2014 1:35 PM âś…Answered | 2 votes
Here is how you access the XML DOM to get the specified node.
string xmlpath = "C:\\inetpub\\BlogEngine.NET\\admin\\comments\\web.config";
XmlDocument d = new XmlDocument();
d.Load(xmlpath);
if (d.DocumentElement != null)
{
XmlNode node = d.DocumentElement.SelectSingleNode("//node[@label='Comments']/node[@label='Web.Config']");
if (node != null)
{
//Process the node
}
}
If this code helps you solve the issue, please mark it as an answer.