Introduction
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)
{
do
{
ParentNode = xmlNode.ParentNode.Name.ToString();
NewPath = "//" + ParentNode + NewPath;
xmlNode = xmlElement.SelectSingleNode("//" + ParentNode);
} while (xmlElement.Name != ParentNode);
NewPath += getXpath;
}
else
{
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");
PopulateDatas();
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.