How to create a TreeView from XML File
Click here to Print Source Codeusing System;
using System.Data;
using System.Windows.Forms;
using System.Xml;
using System.IO;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
XmlDataDocument xmldoc = new XmlDataDocument();
XmlNode xmlnode ;
FileStream fs = new FileStream("tree.xml", FileMode.Open, FileAccess.Read);
xmldoc.Load(fs);
xmlnode = xmldoc.ChildNodes[1];
treeView1.Nodes.Clear();
treeView1.Nodes.Add(new TreeNode(xmldoc.DocumentElement.Name));
TreeNode tNode ;
tNode = treeView1.Nodes[0];
AddNode(xmlnode, tNode);
}
private void AddNode(XmlNode inXmlNode, TreeNode inTreeNode)
{
XmlNode xNode ;
TreeNode tNode ;
XmlNodeList nodeList ;
int i = 0;
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(xNode, tNode);
}
}
else
{
inTreeNode.Text = inXmlNode.InnerText.ToString();
}
}
}
} C# XML - Related Contents
- How to XML in C#
- How to create an XML file in C#
- How to open and read XML file in C# , XmlReader , XmlTextReader , XmlDocument
- How to create XML file from Dataset
- How to Open and read an XML file to Dataset
- How to create an XML file from SQL
- How to search in a XML file
- How to filter in a XML file
- How to insert data from XML to database
- How to create Excel file from XML
- How to create XML file from Excel
- How to XML to DataGridView
- How to create a Crystal Reports from XML File
- XML Serialization Tutorial
- XML Serialization
- XML DeSerialization