Sep 21, 2011, 5:46 AM
Bind xml to treepanel
Hi friends,
I created a xml file from datatable can u tell me how can i bind this xml file to ext.net treepanel..
here is the code to generate xml from datatable (i took this code from other website and works for asp.net treeview control.)
I created a xml file from datatable can u tell me how can i bind this xml file to ext.net treepanel..
here is the code to generate xml from datatable (i took this code from other website and works for asp.net treeview control.)
// Create the DataTable and columns
DataTable ItemTable = new DataTable("MyTable");
ItemTable.Columns.Add("ID" , typeof(int ));
ItemTable.Columns.Add("ParentID", typeof(int ));
ItemTable.Columns.Add("Name" , typeof(String));
ItemTable.Columns.Add("Level", typeof(int));
// add some test data
ItemTable.Rows.Add(new object[] { 0,-1, "Bill Gates",1 });
ItemTable.Rows.Add(new object[] { 1, 0, "Steve Ballmer",2 });
ItemTable.Rows.Add(new object[] { 3, 1, "Mary Smith",3 });
ItemTable.Rows.Add(new object[] { 2, 0, "Paul Allen",2 });
ItemTable.Rows.Add(new object[] { 4, 2, "Ahmed Jones",3 });
ItemTable.Rows.Add(new object[] { 5, 2, "Wing Lee",3 });
// Use the Select method to sort the rows by ParentID
DataRow[] SortedRows;SortedRows = ItemTable.Select("", "ParentID");
// create an XmlDocument (with an XML declaration)
XmlDocument XDoc = new XmlDocument();
XmlDeclaration XDec = XDoc.CreateXmlDeclaration("1.0", null, null);
XDoc.AppendChild(XDec);
// iterate through the sorted data
// and build the XML document
foreach (DataRow Row in SortedRows)
{
// create an element node to insert
// note: Element names may not have spaces so use ID
// note: Element names may not start with a digit so add underscore
XmlElement NewNode = XDoc.CreateElement("_" + Row["ID"].ToString());
NewNode.SetAttribute("ID" , Row["ID"].ToString());
NewNode.SetAttribute("ParentID", Row["ParentID"].ToString());
NewNode.SetAttribute("FullName", Row["Name"].ToString());
NewNode.SetAttribute("Level", Row["Level"].ToString());
// special case for top level node
if ((int)Row["ParentID"] == -1)
XDoc.AppendChild(NewNode);
// root node
else {
// use XPath to find the parent node in the tree
String SearchString;
SearchString = String.Format("//*[@ID=\"{0}\"] ", Row["ParentID"].ToString());
XmlNode Parent = XDoc.SelectSingleNode(SearchString);
if (Parent != null)
Parent.AppendChild(NewNode);
else {
// Handle Error: Employee with no boss
}
}
}
// we cannot bind the TreeView directly to an XmlDocument
// so we must create an XmlDataSource and assign the XML text
XmlDataSource XDdataSource = new XmlDataSource();
XDdataSource.ID = DateTime.Now.Ticks.ToString();
// unique ID is required
XDdataSource.Data = XDoc.OuterXml;
the ouput of the above code will look like this...<?xml version="1.0"?>
<_0 ID="0" ParentID="-1" FullName="Bill Gates" Level="1">
<_1 ID="1" ParentID="0" FullName="Steve Ballmer" Level="2">
<_3 ID="3" ParentID="1" FullName="Mary Smith" Level="3"/>
</_1>
<_2 ID="2" ParentID="0" FullName="Paul Allen" Level="2">
<_4 ID="4" ParentID="2" FullName="Ahmed Jones" Level="3"/>
<_5 ID="5" ParentID="2" FullName="Wing Lee" Level="3"/>
</_2>
</_0>
please help me ..how can i bind this data to ext tree control