Question

I want to bind a xml external file to a TreeView, but I want to use the same XMLDataSource to fill other controls.

So first I want to know what's the best way to do that. Because I think that I will limit the datasource in case of applying a XPath or TransformFile to the DataSource and I will not be able to use it in other controls.

Next, if there's a way of specifying the root node of that xml to bind to the TreeView and if it is possible to hide some of the nodes.

Here is what I've done so far.

<asp:TreeView ID="TreeView1" runat="server" DataSourceID="XmlDataSource1" ExpandDepth="1" >
    <DataBindings >
            <asp:TreeNodeBinding DataMember="header"  TextField="title"/>
            <asp:TreeNodeBinding DataMember="group"  TextField="name"/>
            <asp:TreeNodeBinding DataMember="item"  TextField="text" />
    </DataBindings>                                                       
</asp:TreeView>
Was it helpful?

Solution

The answer to your question is in doing these two things:

  1. Read XML into a reusable object. I could think of DataSet as the best alternative for you. That way data once read into the dataset can be reused to bind other objects as well. Here is link where you can find an example of how to read XML into a data set. http://www.codeproject.com/Articles/10427/Convert-XML-Data-to-DataSet-and-Back

  2. The other part is bind the dataset to the tree view, here is an example for that: http://www.codeproject.com/Articles/25753/Binding-the-ASP-NET-TreeView-to-a-DataSet-or-an-Ob

the issue with the second part could be your data set structure may not be appropriate for binding the tree view in that case you can filter the data further.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top