Community-Credit.com | NonProfitWays.com | SOAPitstop.com   Skin:   
      User: Not logged in 
Home
Newsletter Signup
XSLT Library
Latest XML Blogs
Featured Examples
Presentations
Featured Articles
Book Chapters
Training Courses
Events
NewsGroups
 
Discussions
Examples
Tutorials
Tools
Articles
Resources
Websites
 
Sign In
My Profile
My Articles
My Examples
My Favorites
My Resources
Add a Resource
Logout
 
About Me
My Blog
HeadGeek Articles
Talking Portfolio
Resume
Pictures
World Trip Pics

populating tree view control from an xml file.
Author: phanindra yerra
Published: 6/23/2006 5:33 AM
Category: C#
Summary: This article gives you the code for populating tree view control from an xml file.

 

Introduction

       

            Last few days back. I populated a tree view from xml file.

I think it may useful for so many developers. So I want to provide code for you. Here just change the file path and use it in your applications.

 

     

 

Code:

 

    private void get_Click(object sender, System.EventArgs e)

            {

                  try

                  {

 

                        string _XPath = "xml";

                        string _rootNode = "Tree Demo";

                        string _filePath = @"e:\\x.xml";

 

 

                       

                        // Load the XML file .

 

                        XmlDocument xd   = new XmlDocument();

                        xd.Load(_filePath);

 

                        // Load the XML into the TreeView.

 

 

                        studentsTree.Nodes.Clear();

                        studentsTree.Nodes.Add(new TreeNode(_rootNode));

                        TreeNode tn = new TreeNode();

                        tn          = studentsTree.Nodes[0];

 

                        XmlNodeList nodes = xd.SelectNodes(_XPath);

                        XmlNode xnode      = nodes.Item(0).ParentNode;

 

                        AddNode(ref xnode, ref tn);

 

                        studentsTree.CollapseAll();

                        studentsTree.Nodes[0].Expand();

                  }

 

                  catch (Exception ex)

                  {

                        MessageBox.Show(ex.Message, "Error");

                  }    

 

            }

 

 

 

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();

                  }

            }

 

 

private void button3_Click(object sender, System.EventArgs e)

            {

                  if (button3.Text == "Expand")

                  {

                        studentsTree.ExpandAll();

                        button3.Text = "Collapse";

                  }    

                  else

                  {

                        studentsTree.CollapseAll();

                        studentsTree.Nodes[0].Expand();

                      button3.Text = "Expand";

                  }

 

            }

 

Summary:

 

       This article gives you the code for populating tree view control from an xml file.

 


 

 
Fans of "The Office"
Dwight Bobbleheads are here!
  “It's me! I'm the bobblehead! Yes!”



Advertise on XMLPitstop

Advertise on XMLPitstop


EggHead Cafe
Web Servicee development
DotNetSlackers
Buy text ads here!
internet server backup
Color Laser Printer
Versace sunglasses
Skype vs. sipcall
VoIP Internettelefonie
Buy text ads here!

Interested in Text ads?
2,223 Total Members
36 members(last 30 days)
8 members(last 7 days)
2 members(today)

1,609 Total Discussions
12 Posts(last 30 days)
0 Posts(last 7 days)
0 Posts(today)

17,260 Total Blog Posts
1,839 Blogs(last 30 days)
342 Blogs(last 7 days)
49 Blogs(today)

8,699 Newsgroup Posts
0 Posts(last 30 days)
0 Posts(last 7 days)
0 Posts(today)

13,786 Total Resources
5 Resources(last 30 days)
1 Resources(last 7 days)
0 Resources(today)


 

David Silverlight's XMLPitstop.com| 2801 Florida Ave #225|Miami, FL 33133|Ph:305-447-1139