0

I am trying to populate a TreeView in C# from an XML file using XDocument (LINQ to XML) I tried using this link.

When I failed to understand how the code in the link works I just copied it to my project and changed the necessary variables but it returns strange result on the TreeView

The XML file that I am using is for making folders but the folders are easy to create because you can extract the path easily from the XML document.

Here is what the file looks like:

<?xml version="1.0" encoding="utf-8"?>
<dir name="After">
  <dir name="Site Documents">
    <dir name="02. External">
      <dir name="1. Mechanical">
        <dir name="01. Submittals">
          <dir name="1. Sent" />
          <dir name="2. Received" />
        </dir>
        <dir name="02. Drawings">
          <dir name="1. Sent" />
          <dir name="2. Received" />
        </dir>
        <dir name="03. MIR">
          <dir name="1. Sent" />
          <dir name="2. Received" />
        </dir>
        <dir name="04. IR">
          <dir name="1. Sent" />
          <dir name="2. Received" />
        </dir>
        <dir name="05. RFI">
          <dir name="1. Sent" />
          <dir name="2. Received" />
        </dir>
        <dir name="06. DFC">
          <dir name="1. Sent" />
          <dir name="2. Received" />
        </dir>
        <dir name="07. PVN">
          <dir name="1. Sent" />
          <dir name="2. Received" />
        </dir>
      </dir>
      <dir name="2. Electrical">
        <dir name="01. Submittals">
          <dir name="1. Sent" />
          <dir name="2. Received" />
        </dir>
        <dir name="02. Drawings">
          <dir name="1. Sent" />
          <dir name="2. Received" />
        </dir>
        <dir name="03. MIR">
          <dir name="1. Sent" />
          <dir name="2. Received" />
        </dir>
        <dir name="04. IR">
          <dir name="1. Sent" />
          <dir name="2. Received" />
        </dir>
        <dir name="05. RFI">
          <dir name="1. Sent" />
          <dir name="2. Received" />
        </dir>
        <dir name="06. DFC">
          <dir name="1. Sent" />
          <dir name="2. Received" />
        </dir>
        <dir name="07. PVN">
          <dir name="1. Sent" />
          <dir name="2. Received" />
        </dir>
      </dir>
    </dir>
      <dir name="03. Internal">
    <dir name="01. PR">
      <dir name="1. MECH">
      </dir>
      <dir name="2. ELEC" />
    </dir>
    <dir name="02. PO">
    </dir>
    <dir name="03. SRF">
    </dir>
    <dir name="04. RMR" />
  </dir>
  </dir>

</dir>

The result in the treeview appears as:

TreeView Result

1 Answer 1

1

Using a Windows Form with XML Linq and recursion :

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        const string FILENAME = @"c:\temp\test.xml";
        public Form1()
        {
            InitializeComponent();
            XDocument doc = XDocument.Load(FILENAME);
            XElement dir = doc.Root;
            TreeNode node = new TreeNode((string)dir.Attribute("name"));
            treeView1.Nodes.Add(node);
            GetTree(dir, node);
            treeView1.ExpandAll();
        }
        public static void GetTree(XElement dir, TreeNode node)
        {
            foreach (XElement child in dir.Elements("dir"))
            {
                TreeNode childNode = new TreeNode((string)child.Attribute("name"));
                node.Nodes.Add(childNode);
                GetTree(child, childNode);
            }
        }
    }
}

enter image description here

Sign up to request clarification or add additional context in comments.

3 Comments

I see, the .Attribute("name") was what I missed. Its working fine now thanks.
I was wondering how treeView1.Nodes.Add(node); is before GetTree(dir, node); and everything is working fine ?!. Shouldn't treeView1.Nodes.Add(node); be after GetTree(dir, node);
I doesn't make a difference. Either way will work. node is an object with children and it doesn't make a difference when you add the object to the parent the child nodes are always the same and do not change.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.