Binding Tree View with XML
A sample code to show the binding of a Tree View with an XML file using XPATH.
Here is a sample Tree View, for which a XML data source is used so that we can bind the XML file. The file name is present in the application folder which is associated with the XPATH.
The Main Control used for this are..
XmlDataSource :- This is used to bind any data bound control with a XML document. Here we can specify the name of the File as well as the name of the XML node we want to bind.
TreeView : The control in which we have to bind our XML file, the important attributes of the control as
DATAFILE- Takes the XML file name which we want to bind.
ID - Id of that control
DataSource - It take the data source from where we want to bind the control.
And the List items Which we want to display as child. that too we can bind using the XPATH.
<asp:XmlDataSource ID="SampleSource" runat="server" XPath="/PageTitle" DataFile="~/App_Data/FileName.xml" />
<asp:TreeView ID="myTreeView" runat="server" DataSourceID=" SampleSource">
<asp:TreeNodeBinding DataMember="Id" TextField="#InnerText" />
<asp:TreeNodeBinding DataMember="Tite" TextField="#InnerText" />
<asp:TreeNodeBinding DataMember="PageUrl" TextField="#InnerText" />
In the page load event we do not need to do much as we already assign the respected file name and the Node to the Tree view we simply have to associated the file with the control, for this simply write this inside the page load.
protected void Page_Load(object sender, EventArgs e)
SampleSource.XPath = "/FileName";