0
votes

In my current project (winform), i am feeding a treeview with an xml. The xml is build up like this example:

<Root>
  <ns code="NS" description=" - New">
    <someName code="5M" description=" - some 5M">
      <entry1 code="TN" description=" - some description 1" />
      <entry2 code="TO" description=" - some description 2" />
    </someName>
	<someOtherName code="6M" description=" - some 6M">
		<entry1 code="TN" description=" - some description 1" />
		<entry2 code="TO" description=" - some description 2" />
	</someOtherName>
  </ns>
</Root>

Im populating my treeview based upon the code and description, so the treeview looks like this: enter image description here

But now i wand to be able to add nodes to the xml based upon the selected node in the treeview.

Now i am wondering, is it somehow possible to get the xmlNode, based upon the selected Treenode (that is based upon non-unique attributes).

What would be the best practice to accomplish this ?

1

1 Answers

0
votes

You could use the property "FullPath" of the selected tree node:

  private void trvAcsConfig_AfterSelect(object sender, TreeViewEventArgs e)
    {
        try
        {
            //get selected tree node
            TreeNode selectedTreeNOde = ((TreeView)sender).SelectedNode;

            string fullPath = "/" + selectedTreeNOde.FullPath.Replace("\\", "/");

            //selected tree node is an attribute, comment,... 
            if (selectedTreeNOde.Nodes.Count == 0)
                fullPath = fullPath.Substring(0, fullPath.LastIndexOf('/'));


            XmlNodeList nodes = AcsConfig.ConfigXmlDocument.SelectNodes(fullPath);

            Console.WriteLine(selectedTreeNOde.Name);
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
    }