xpath in c#

J

juli jul

Hello,
Here is my try to show all the Group elements and it subelements in a
tree view. I think something with the x-path implementation is wrong
because I don't see any result although there are such elements,could
you please tell me what is wrong?
The Code:

private void populateTreeControl(
System.Xml.XmlNode document,
System.Windows.Forms.TreeNodeCollection nodes)
{
XmlNodeList xmn = document.SelectNodes("/sectionGroup/*");

foreach (System.Xml.XmlNode node in
xmn)
{


string text = (node.Value != null ? node.Value :
(node.Attributes != null &&
node.Attributes.Count > 0) ?
node.Attributes[0].Value : node.Name);
TreeNode new_child = new TreeNode(text);
nodes.Add(new_child);
populateTreeControl(node, new_child.Nodes);


}
}

Thanks a lot!
 
N

Nicholas Paldino [.NET/C# MVP]

Juli,

I think that your x-path expression is incorrect. You shouldn't have
the slash in the beginning, like this:

XmlNodeList = xmn = document.SelectNodes("sectionGroup\*");

Also, notice the orientation of the slash. I believe that is wrong as
well.

Hope this helps.
 
J

juli jul

Hello,thanks but it didn't helo it is not being compiled. Is there some
other way to fix it?
Thanks!
 
J

juli jul

Hello,thanks but it didn't helo it is not being compiled. Is there some
other way to fix it?
Thanks!
 
J

James Curran

Ignore Nicholas (this time only). Both of his suggestion are wrong.

You want to start with two slashes:

XmlNodeList xmn = document.SelectNodes("//sectionGroup/*");
 
R

Robbe Morris [C# MVP]

To make this faster, you want to pass in an XmlNode and TreeNode
as parameters instead of XmlDocument and TreeNodeCollection

Then, you can restrict your query to only those nodes under the parent node.
The previous two posts will still query the entire document.

XmlNodeList list = node.SelectNodes("./sectionGroup");
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top