Taher
Taher

Reputation: 593

C# Dynamic TreeView population from XML using (LINQ to XML)

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

Upvotes: 0

Views: 588

Answers (1)

jdweng
jdweng

Reputation: 34429

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

Upvotes: 1

Related Questions