ASP.NET - xml to treeview using dataset - Asked By asif hameed on 15-May-12 10:48 AM

I have an xml file with following structure:

<table name="tblcats">
        <Industry>Fortune 100</Industry>


I want to populate a treeview using this xml. I have created a dataset and sorted it and written following code:

   Dim xmlfile As String = Server.MapPath("~/App_Data/Industries.xml")
        Dim ds As New DataSet()

        Dim sortedRows As DataRow()
        sortedRows = ds.Tables(1).Select("", "ParentId")

        Dim XDoc As New XmlDocument()
        Dim XDec As XmlDeclaration = XDoc.CreateXmlDeclaration("1.0", Nothing, Nothing)

        ' iterate through the sorted data
        ' and build the XML document
        For Each Row As DataRow 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
            Dim NewNode As XmlElement = XDoc.CreateElement("_" & Row("Id").ToString())
            NewNode.SetAttribute("Id", Row("Id").ToString())
            NewNode.SetAttribute("ParentId", Row("ParentId").ToString())
            NewNode.SetAttribute("Industry", Row("Industry").ToString())

            ' special case for top level node
            If CInt(Row("ParentId")) = -1 Then
                ' root node
                ' use XPath to find the parent node in the tree
                Dim SearchString As [String]
                SearchString = [String].Format("//*[@Id=""{0}""] ", Row("ParentId").ToString())
                Dim Parent As XmlNode = XDoc.SelectSingleNode(SearchString)

                If Parent IsNot Nothing Then

                    ' Handle Error: Employee with no boss
                End If
            End If

        ' we cannot bind the TreeView directly to an XmlDocument
        ' so we must create an XmlDataSource and assign the XML text
        Dim XDdataSource As New XmlDataSource()
        XDdataSource.ID = DateTime.Now.Ticks.ToString()
        ' unique ID is required
        XDdataSource.Data = XDoc.OuterXml

        ' we want the full name displayed in the tree so 
        ' do custom databindings
        Dim Binding As New TreeNodeBinding()
        Binding.TextField = "FullName"
        Binding.ValueField = "ID"

        ' Finally! Hook that bad boy up!       
        TreeView1.DataSource = XDdataSource

but It fails here:

 SearchString = [String].Format("//*[@Id=""{0}""] ", Row("ParentId").ToString())

How can I fix this xPath to match my XML ? Please suggest how can I fix this issue

kalpana aparnathi replied to asif hameed on 15-May-12 03:22 PM

You can bind data like this:

      ds.TransformFile = Server.MapPath("yourfile.xsl");
      TreeView1.DataSource = ds;

[)ia6l0 iii replied to asif hameed on 15-May-12 09:42 PM
It looks like you are trying to fix a copy-pasted code. Nevertheless, the xml would have been much different than the one that you use. 

Couple of things that I noticed in a glance. 

a) Id is not an attribute. It is an element. So in the search string, you should avoid the @ symbol.
SearchString = [String].Format("//*[Id=""{0}""] ", Row("ParentId").ToString())

b) You could have done something on the following line to retrieve the Id value. 
from doc in xDoc.Descendants("row")
                                        select new row
                                            Id = Convert.ToInt32(doc.Element("Id").Value),
                                            Industry = doc.Element("Industry").Value,
                                            ParentId = DateTime.Parse(doc.Element("ParentId").Value)
                                        }.where (doc.Element("Id").value == Row("ParentId")

Hope this helps you proceed.
Jitendra Faye replied to asif hameed on 16-May-12 12:48 AM
With reference of-

Try this code-

 DataSet dataset = new DataSet();
XmlDataSource ds = new XmlDataSource();
string data = dataset.GetXml();
= "&lt;tblFeature><FeatureName>Invoice</FeatureName><FeatureName>Meetings</FeatureName></tblFeature>";

.Data = data;
.TransformFile = Server.MapPath("xslt61.xsl");;

TreeView1.DataSource = ds;