---
title: "How to create a TreeView from XML File in C#"  
description: "How to create a TreeView from XML File in C#"  
author: "Pravesh Singh"  
published: 2013-06-15  
updated: 2013-06-15  
canonical: https://www.mindstick.com/forum/1180/how-to-create-a-treeview-from-xml-file-in-c-sharp  
category: "c#"  
tags: ["c#"]  
reading_time: 1 minute  

---

# How to create a TreeView from XML File in C#

Hi [Developers](https://www.mindstick.com/articles/12875/blunders-you-must-avoid-while-hiring-java-developers-to-get-the-best-fulfilling-your-needs),\
How to create a [TreeView](https://www.mindstick.com/forum/12671/how-to-get-index-of-all-the-nodes-in-a-treeview) from [XML](https://www.mindstick.com/blog/203/working-with-xml-data-in-sql-server) [File in C#](https://www.mindstick.com/forum/159903/how-to-create-a-log-file-in-c-sharp)?\
Thanks in advance.

## Replies

### Reply by Sumit Kesarwani

Hi Pravesh,\
\
You can use this code:

```
using 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();            }        }    }}
```


---

Original Source: https://www.mindstick.com/forum/1180/how-to-create-a-treeview-from-xml-file-in-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
