Reading XML document in treeview (C# winform)

I have the following xml source file:

< xml version="1.0" encoding="utf-8" >
<course id="2555" title="Developing Microsoft .NET Applications for Windows (Visual C#

.NET)" length="5 days" source="http://www.microsoft.com/learning/syllabi/en-

us/2555Afinal.mspx">
 <module id="1" title="Introducing Windows Forms" location="D:\Disk-C\Documents

and Settings\orit_itzhar.ATRICA\My Documents\XML\c#">
  <lesson id="1.1">
   <subject>Creating a Form</subject>
   <file>Introducing_Windows_Forms_Course-2555-Module-1.pdf</file>
  </lesson>
  <lesson id="1.2">
   <subject>Adding Controls to a Form</subject>
   <file>type&amp;exceptions.pdf</file>
  </lesson>
  <lesson id="1.3">
   <subject>Creating an Inherited Form</subject>
   <file>winforms.pdf</file>
  </lesson>
  <lesson id="1.4">
   <subject>Organizing Controls on a Form</subject>
   <file>BnsLrnCs.zip</file>
  </lesson>
  <lesson id="1.5">
   <subject>Creating MDI Applications</subject>
   <file>Working_with_Controls_Course-2555-Module-2.pdf</file>
  </lesson>
  <lab id="1.1">
   <exercise id="1.1.1">Creating a New Windows Form</exercise>
   <exercise id="1.1.2">Inheriting a New Form from an Existing

Windows Form</exercise>
  </lab>
 </module>
  <module id="2" title="Working with Controls" location="D:\Disk-

C\Documents and Settings\orit_itzhar.ATRICA\My Documents\XML\c#">
  <lesson id="2.1">
   <subject>Creating an Event Handler for a Control</subject>
   <file>Introducing_Windows_Forms_Course-2555-Module-1.pdf</file>
  </lesson>
  <lesson id="2.2">
   <subject>Using Windows Forms Controls</subject>
   <file>type&amp;exceptions.pdf</file>
  </lesson>
  <lesson id="2.3">
   <subject>Using Dialog Boxes in a Windows Forms

Application</subject>
   <file>winforms.pdf</file>
  </lesson>
  <lesson id="2.4">
   <subject>Adding Controls at Run Time</subject>
   <file>BnsLrnCs.zip</file>
  </lesson>
  <lesson id="2.5">
   <subject>Creating Menus</subject>
   <file>Working_with_Controls_Course-2555-Module-2.pdf</file>
  </lesson>
  <lesson id="2.6">
   <subject>Validating User Input</subject>
   <file>Working_with_Controls_Course-2555-Module-2.pdf</file>
  </lesson>
  <lab id="2.1">
   <exercise id="2.1.1">Creating and Using Controls</exercise>
  </lab>
 </module>
</course>


I want to display in hierarchy similar to the one in the XML file, a table of content

of the course in a treeview in a winform in C# program.
I want that the xpath will take at the top level from the "course" nodes, the "title"

attribute, then from the "module" node, the "title" attribute, and from under the

lessons, the values of the "subject" node for each lesson at the 3rd level.
I expect something that will look like:

Developing Microsoft .NET Applications for Windows (Visual C# .NET)
___Introducing Windows Forms
______Creating a Form
______Adding Controls to a Form
______Creating an Inherited Form
etc...

Can someone please assist

Thanks



Answer this question

Reading XML document in treeview (C# winform)

  • JRClark

    Hi,

    you can use the following function, you need to pass the path of your XML file and the Reference of your TreeView that you probably have in your form, it will populate the TreeView for you.

    You can also write this recursilvely if you want




    private
    void PopulateTreeView(string xmlFile, System.Windows.Forms.TreeView tree)
    {
       
    try
       {
          TreeNode t1,t2;
          if (xmlFile != null && tree != null
          {
             XmlDocument doc =
    new XmlDocument();
             doc.Load(xmlFile);
             foreach(XmlNode node in doc.SelectNodes("//course"))
             {
                t1 =
    new TreeNode(node.Attributes["title"].Value);
                tree.Nodes.Add(t1);
                foreach(XmlNode nod in node.SelectNodes("//module"))
                {
                   t2 =
    new TreeNode(nod.Attributes["title"].Value);
                   t1.Nodes.Add(t2);
                   foreach(XmlNode n in nod.SelectNodes("//subject"))
                   {
                      t2.Nodes.Add(
    new TreeNode(n.FirstChild.Value));
                   }
                }
             }
          }
       }

       catch(Exception exp)
       {
          MessageBox.Show(exp.Message);
       }
    }


     


  • Gianluca Pezzoli

    Thanks so much Aleem, the function promoted me so much. I made few modifications and that's exactly what I needed.

    My next step is to link each subject to the file that goes with him.
    For example, the subject in lesson id 1.1 is "Creating a Form", it should go to the file that resides with him under the lesson: "Introducing_Windows_Forms_Course-2555-Module-1.pdf", while the files resides under the location that is specified under the "module" tag - specifically: "D:\Documents and Settings\orit_itzhar\My Documents\XML\csharp" so I want the item of subject of lesson id 1.1 to open the file: D:\Documents and Settings\orit_itzhar\My Documents\XML\csharp\Introducing_Windows_Forms_Course-2555-Module-1.pdf

    Any suggestion how to do that
    Thanks a lot.

    The
     
    xml again:


    < xml version="1.0" encoding="utf-8" >

    <course id="2555" title="Developing Microsoft .NET Applications for Windows (Visual C# .NET)" length="5 days" source="http://www.microsoft.com/learning/syllabi/en-us/2555Afinal.mspx">

    <module id="1" title="Introducing Windows Forms" location="D:\Documents and Settings\orit_itzhar\My Documents\XML\csharp">

    <lesson id="1.1">

    <subject>Creating a Form</subject>

    <file>Introducing_Windows_Forms_Course-2555-Module-1.pdf</file>

    </lesson>

    <lesson id="1.2">

    <subject>Adding Controls to a Form</subject>

    <file>type&amp;exceptions.pdf</file>

    </lesson>

    <lesson id="1.3">

    <subject>Creating an Inherited Form</subject>

    <file>winforms.pdf</file>

    </lesson>

    <lesson id="1.4">

    <subject>Organizing Controls on a Form</subject>

    <file>BnsLrnCs.zip</file>

    </lesson>

    <lesson id="1.5">

    <subject>Creating MDI Applications</subject>

    <file>Working_with_Controls_Course-2555-Module-2.pdf</file>

    </lesson>

    <lab id="1.1">

    <exercise id="1.1.1">Creating a New Windows Form</exercise>

    <exercise id="1.1.2">Inheriting a New Form from an Existing Windows Form</exercise>

    </lab>

    </module>

    <module id="2" title="Working with Controls" location="D:\Disk-C\Documents and Settings\orit_itzhar.ATRICA\My Documents\XML\c#">

    <lesson id="2.1">

    <subject>Creating an Event Handler for a Control</subject>

    <file>Introducing_Windows_Forms_Course-2555-Module-1.pdf</file>

    </lesson>

    <lesson id="2.2">

    <subject>Using Windows Forms Controls</subject>

    <file>type&amp;exceptions.pdf</file>

    </lesson>

    <lesson id="2.3">

    <subject>Using Dialog Boxes in a Windows Forms Application</subject>

    <file>winforms.pdf</file>

    </lesson>

    <lesson id="2.4">

    <subject>Adding Controls at Run Time</subject>

    <file>BnsLrnCs.zip</file>

    </lesson>

    <lesson id="2.5">

    <subject>Creating Menus</subject>

    <file>Working_with_Controls_Course-2555-Module-2.pdf</file>

    </lesson>

    <lesson id="2.6">

    <subject>Validating User Input</subject>

    <file>Working_with_Controls_Course-2555-Module-2.pdf</file>

    </lesson>

    <lab id="2.1">

    <exercise id="2.1.1">Creating and Using Controls</exercise>

    </lab>

    </module>

    </course>



     


  • Reading XML document in treeview (C# winform)