Click here to Skip to main content
15,897,226 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a string variable that returns this:

HTML
<pre><role name="olvers" guid="ee81e26aeb7cd" description="Used at" isdynamic="false">
<extradata>
</extradata>
<include>
 <users>
   <user name="Q:www\sitename1" />
   <user name="Q:www\sitename2" />
   <user name="Q:"www\sitename3" />
 </users>
<groups />
<smartobjects />
</include>
<exclude>
<users />
<groups />
<smartobjects />
</exclude>
</role>



I want to be able to extract the name attribute from each user element and store it in a list. In this case, I'd want the list to contain the following values:

Q:www\sitename1
Q:www\sitename2
Q:www\sitename3

Unfortunately, this doesn't work. How can I extract these values and store them in a list?

What I have tried:

XElement documentRoot = XElement.Load(getUserRoles);


XmlNodeList nodes = 
            root.SelectNodes("/role/extradata/include/users");
foreach (XmlElement node in nodes)
{
    log.Info("User in role : " + node.Value);
}
Posted
Updated 12-Dec-17 7:18am

Shouldn't that select be "/role/include/users" ... include is not a sub-node of extradata
 
Share this answer
 
Note that I changed a few things, not just the path.

XmlDocument documentRoot = new XmlDocument();
documentRoot.LoadXml(getUserRoles);

XmlNodeList nodes = documentRoot.SelectNodes("/role/include/users/user");
foreach (XmlElement node in nodes)
{
    log.Info("User in role : " + node.Attributes["name"].Value);
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900