What the...?

Something bad happened

We're not sure what, but we have a few guesses.

Problem: Object reference not set to an instance of an object.
Ticket: 7864825
Server: Web02
Bind XML TreeView with GridView/DataGrid - CodeProject Click here to Skip to main content
11,715,043 members (71,758 online)
Click here to Skip to main content

Bind XML TreeView with GridView/DataGrid

, 22 Nov 2006 CPOL 53.7K 735 18
Rate this:
Please Sign up or sign in to vote.
This sample code is used to populate XML data into a GridView with a TreeView format.

Sample screenshot


This sample program is used to populate XML data into a GridView control in the format of a TreeView.

Just pass the XML document path and it automatically finds the root node and child nodes. When you click the root node, it finds the child node and displays on the grid vice versa.

Here is the function to find the root node:

public string FindParentNode(XmlDocument xmlDocument, string xPath)
    XmlNode xmlNode;
    xmlElement = doc.DocumentElement;
    xmlNode = xmlElement.SelectSingleNode(getXpath);
    NewPath = string.Empty;
    if (xmlElement.Name != xmlNode.Name)
            ParentNode = xmlNode.ParentNode.Name.ToString();
            NewPath = "//" + ParentNode + NewPath;
            xmlNode = xmlElement.SelectSingleNode("//" + ParentNode);
        } while (xmlElement.Name != ParentNode);
        NewPath += getXpath;
        NewPath = xPath;
    return NewPath;

This function is used to find the root of the XPpath, and this path is assigned to the new XML path.

XmlDocument xmlDocument = new XmlDocument();
int j;
j = GetXMLPathList(@" E:\download\XML - Sathya\XMLSamples\XML\emp.xml");

The above code is used to define the XML path. This single line will do the whole process of binding data into the GridView with tree format. The GridView control is similar to the TreeView control; you can expand and collapse the tree structure and find data.

This is my first article for CodeProject. Give me your ideas and suggestions about this code.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Web Developer
United States United States
I have 5+ years of experience in Microsoft Technologies and have 2+ years of experience in .NET Technologies. Currenty I am woring in Visualsoft Technologies, Hyderabad and involed in Motorola projects. I feel in my carrier it's good projects. Because in this projects we are implementing the VS2005 and .NET 3.0 with Sqlserver 2005, also winfx.
SQL Server is my favourate subject, I was worked one ERP project as DBA. So it's was nice experience for me in SQL Server and
got BrainBench Certificate for .NET,DataBase and C#.

You may also be interested in...

Comments and Discussions

GeneralMy vote of 1 Pin
Dmitri Nesteruk6-Jun-10 6:27
memberDmitri Nesteruk6-Jun-10 6:27 
GeneralHi Pin
saritha_codeproject12-Oct-09 23:15
membersaritha_codeproject12-Oct-09 23:15 
GeneralVB code needed Pin
abdallah22233319-May-09 1:25
memberabdallah22233319-May-09 1:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150901.1 | Last Updated 22 Nov 2006
Article Copyright 2006 by RajaramanIndia
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid