xpath in c#

  • Thread starter Thread starter juli jul
  • Start date Start date
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!
 
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.
 
Hello,thanks but it didn't helo it is not being compiled. Is there some
other way to fix it?
Thanks!
 
Hello,thanks but it didn't helo it is not being compiled. Is there some
other way to fix it?
Thanks!
 
Ignore Nicholas (this time only). Both of his suggestion are wrong.

You want to start with two slashes:

XmlNodeList xmn = document.SelectNodes("//sectionGroup/*");
 
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");
 
Back
Top