none
Populate Treeview control from an XML file using C# RRS feed

  • Question

  • here is my XML  File

    <?xml version="1.0" encoding="utf-8" ?>
    <AditiServers xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
      <Servers>
        <Item ServerID="1">
          <ServerName>irc.quakenet.org</ServerName>
        </Item>
     <Item ServerID="2">
          <ServerName>irc.inspircd.org</ServerName>
        </Item>
      </Servers>
    </AditiServers>

    and here is my code

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Xml;

    namespace Aditi
    {
        public partial class fAditi : Form
        {
            public fAditi()
            {
                InitializeComponent();
            }

            private void fAditi_Load(object sender, EventArgs e)
            {
                try
                {
                    string strXPath = "<ServerName></ServerName>";
                    string strRootNode = "Aditi Server List";
                    string strXMLFile = @"c:\Aditi\Data\AditiServers.xml";

                    // Load the XML file
                    XmlDocument dom = new XmlDocument();
                    dom.Load(strXMLFile);

                    // Load the XML into Treeview.
                    this.treeViewAditiServers.Nodes.Clear();
                    this.treeViewAditiServers.Nodes.Add(new TreeNode(strRootNode));
                    TreeNode tNode = new TreeNode();
                    tNode = this.treeViewAditiServers.Nodes[0];

                    XmlNodeList oNodes = dom.SelectNodes(strXPath);
                    XmlNode xNode = oNodes.Item(0).ParentNode;

                    AddNode(ref xNode, ref tNode);

                    this.treeViewAditiServers.ExpandAll();

                }
                catch (Exception ex)
                {
                }

            }

            private void AddNode(ref XmlNode inXmlNode, ref TreeNode inTreeNode)
            {
                // Recursive routine to walk the XML DOM and add its nodes to a TreeView
                XmlNode xNode;
                TreeNode tNode;
                XmlNodeList nodeList;
                int i;

                // Loop through the XML nodes until the leaf is reached.
                // Add the nodes to the TreeView during the looping process.
                if (inXmlNode.HasChildNodes)
                {
                    nodeList = inXmlNode.ChildNodes;
                    for (i = 0; i <= nodeList.Count - 1; i++)
                    {
                        xNode = inXmlNode.ChildNodes[i];
                        inTreeNode.Nodes.Add(new TreeNode(xNode.Name));
                        tNode = inTreeNode.Nodes[i];
                        AddNode(ref xNode, ref tNode);
                    }
                }
                else
                {
                    inTreeNode.Text = inXmlNode.OuterXml.Trim();
                }

            }
       
       }
    }

    what am I doing wrong?
    All that is displaying is the root node of Aditi Server List
    what i want to display as nodes is the irc server between <ServerName>irc.quakenet.org</ServerName> without it saying <ServerName></ServerName>

    end result to look like the following:

    Aditi Server List
    -irc.quakenet.org
    -irc.inspircd.org

    Tuesday, September 22, 2009 7:23 AM

Answers

  • Here is the code

                string strXPath = "//Servers/Item";
                string strRootNode = "Aditi Server List";
                string strXMLFile = @"C:\Users\Tamer\Desktop\c.xml";
    
                // Load the XML file
                XmlDocument dom = new XmlDocument();
                dom.Load(strXMLFile);
    
                // Load the XML into Treeview.
                this.treeView1.Nodes.Clear();
                this.treeView1.Nodes.Add(new TreeNode(strRootNode));
                TreeNode tNode = new TreeNode();
                tNode = this.treeView1.Nodes[0];
    
                XmlNodeList oNodes = dom.SelectNodes(strXPath);
    
                foreach (XmlNode nd in oNodes)
                {
                    tNode.Nodes.Add(new TreeNode(nd.InnerText));
                }
    
                this.treeView1.ExpandAll();
    Tuesday, September 22, 2009 7:47 AM
  • Your NodeMouseClick Event is not Handled.

    Pls go to treeview control and select the method for event or type the code below to the Designer.cs

    this.treeViewAditiServers.NodeMouseClick += new System.Windows.Forms.TreeNodeMouseClickEventHandler(this.treeViewAditiServers_NodeMouseClick_1);


    • Marked as answer by man-helping-man Wednesday, September 23, 2009 7:35 AM
    Wednesday, September 23, 2009 7:06 AM

All replies

  • Here is the code

                string strXPath = "//Servers/Item";
                string strRootNode = "Aditi Server List";
                string strXMLFile = @"C:\Users\Tamer\Desktop\c.xml";
    
                // Load the XML file
                XmlDocument dom = new XmlDocument();
                dom.Load(strXMLFile);
    
                // Load the XML into Treeview.
                this.treeView1.Nodes.Clear();
                this.treeView1.Nodes.Add(new TreeNode(strRootNode));
                TreeNode tNode = new TreeNode();
                tNode = this.treeView1.Nodes[0];
    
                XmlNodeList oNodes = dom.SelectNodes(strXPath);
    
                foreach (XmlNode nd in oNodes)
                {
                    tNode.Nodes.Add(new TreeNode(nd.InnerText));
                }
    
                this.treeView1.ExpandAll();
    Tuesday, September 22, 2009 7:47 AM
  • thank you very much i been fighting with this for awhile lol
    Tuesday, September 22, 2009 8:01 AM
  • i have now added a status form to the mix and launches a form per server node with the server node text:

    foreach (XmlNode nd in oNodes)
                    {
                        tNode.Nodes.Add(new TreeNode(nd.InnerText));

                        // This is to launch a Status for per server listed in the XML file
                        // with the text of the server name added to the form
                        fStatus fs = new fStatus();
                        fs.MdiParent = this;
                        fs.Text = nd.InnerText;
                        fs.Show();

                    }

    How do i set a tag to each of the forms launched so i can then find them on a click event
    for the treeview and activate the proper form relating to the node?
    How would i set up the click event aswell?, i have tryed many methodes but no success.

    Tuesday, September 22, 2009 10:54 PM
  • You can use treeview's NodemouseClick event

            private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
            {
                Form2 frm = new Form2();
                frm.Tag = e.Node;
                frm.Show();
            }
    Wednesday, September 23, 2009 5:04 AM
  • here is the code with what you had suggested earlyer which works like a charm and if you notice i have added the status form invoking within the code.

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Xml;

    namespace Aditi_2010_GUI
    {
        public partial class fAditi : Form
        {
            public fAditi()
            {
                fStatus fs = new fStatus();

                InitializeComponent();
            }

            private void fAditi_Load(object sender, EventArgs e)
            {
                this.listViewDCC.Hide();
                //this.panelServerTree.Hide();

                AditiServerTree();
            }


            private void AditiServerTree()
            {
                try
                {
                    string strXPath = ("//Servers/Item");
                    string strRootNode = "Aditi Server List";
                    string strXMLFile = @"c:\AditiServers.xml";

                    // Load the XML file
                    XmlDocument dom = new XmlDocument();
                    dom.Load(strXMLFile);

                    // Load the XML into Treeview.
                    this.treeViewAditiServers.Nodes.Clear();
                    this.treeViewAditiServers.Nodes.Add(new TreeNode(strRootNode));
                    TreeNode tNode = new TreeNode();
                    tNode = this.treeViewAditiServers.Nodes[0];

                    XmlNodeList oNodes = dom.SelectNodes(strXPath);

                    foreach (XmlNode nd in oNodes)
                    {
                        tNode.Nodes.Add(new TreeNode(nd.InnerText));

                        // This is to launch a Status form per server listed in the XML file
                        // with the text of the server name added to the form
                        fStatus fs = new fStatus();
                        fs.MdiParent = this;
                        fs.Text = nd.InnerText;
                        fs.Show();

                        //tNode.EnsureVisible();
                    }

                    this.treeViewAditiServers.ExpandAll();

                }
                catch (Exception ex)
                {
                }

            }

            private void AddNode(ref XmlNode inXmlNode, ref TreeNode inTreeNode)
            {
                // Recursive routine to walk the XML DOM and add its nodes to a TreeView
                XmlNode xNode;
                TreeNode tNode;
                XmlNodeList nodeList;
                int i;

                // Loop through the XML nodes until the leaf is reached.
                // Add the nodes to the TreeView during the looping process.
                if (inXmlNode.HasChildNodes)
                {
                    nodeList = inXmlNode.ChildNodes;
                    for (i = 0; i <= nodeList.Count - 1; i++)
                    {
                        xNode = inXmlNode.ChildNodes[i];
                        inTreeNode.Nodes.Add(new TreeNode(xNode.Name));
                        tNode = inTreeNode.Nodes[i];
                        AddNode(ref xNode, ref tNode);
                    }
                }
                else
                {
                    inTreeNode.Text = inXmlNode.OuterXml.Trim();
                }
            }

        }
    }


    now what i am trying to do is since the status forms already spawn how do i click the corresponding irc server and have that form come to the front? i do not need to make a new form i just need to have that form that is already created come to the front as active in front of all the other status forms.

    here is a screenshot so you can get a better idea

    http://img246.imageshack.us/img246/1742/aditiclientwithworkings.png

    so as you can see i have the forms already there i just want to click the treeview node and have the appropriate form come forward.

    Wednesday, September 23, 2009 6:04 AM
  • Here you are

            private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
            {
                foreach (Form frm in this.MdiChildren)
                {
                    if (frm.Text == e.Node.Text)
                    {
                        frm.BringToFront();
                    }
                }
            }
    And, do you still need AddNode method?
    Wednesday, September 23, 2009 6:10 AM
  • here is the complete solution

    http://www.megafileupload.com/en/file/137505/Aditi-2010-GUI-with-treeview-rar.html

    now i have no idea why nothing is working im begining to wonder if there is a visual studio 2010 issue

    again thanks again for your help in advance
    Wednesday, September 23, 2009 6:58 AM
  • Your NodeMouseClick Event is not Handled.

    Pls go to treeview control and select the method for event or type the code below to the Designer.cs

    this.treeViewAditiServers.NodeMouseClick += new System.Windows.Forms.TreeNodeMouseClickEventHandler(this.treeViewAditiServers_NodeMouseClick_1);


    • Marked as answer by man-helping-man Wednesday, September 23, 2009 7:35 AM
    Wednesday, September 23, 2009 7:06 AM
  • holy crapola you are a god.

    /me bows down

    thanks very much.
    Wednesday, September 23, 2009 7:31 AM
  • You're welcome :)
    Wednesday, September 23, 2009 7:42 AM
  • is there a way to chat with you in private?
    Wednesday, September 23, 2009 7:50 AM
  • since I'm not able to open live messenger at work, I am much more online here.

    However if you contact me from my site I can add you to my messenger.
    http://www.tameroz.com/en/contact.aspx

    Thanks.

    Wednesday, September 23, 2009 1:03 PM
  • i have contacted you through your site email as man-helping-man

    thanx again for your time.
    Wednesday, September 23, 2009 7:06 PM