c# - 使用数据集的 xml 到 TreeView

标签 c# asp.net xml vb.net xpath

我有一个具有以下结构的 xml 文件:

<table name="tblcats">
    <row>
        <Id>3680</Id>
        <Industry>Associations</Industry>
        <ParentId>1810</ParentId>
    </row>
    <row>
        <Id>1592</Id>
        <Industry>Fortune 100</Industry>
        <ParentId>1810</ParentId>
    </row>
    <row>

</table>

我想使用此 xml 填充 TreeView 。我创建了一个数据集并对其进行了排序,并编写了以下代码:

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

        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)
        XDoc.AppendChild(XDec)

        ' 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
                XDoc.AppendChild(NewNode)
            Else
                ' 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
                    Parent.AppendChild(NewNode)
                Else


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


        ' 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"
        TreeView1.DataBindings.Add(Binding)

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

但这里失败了:

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

如何修复此 xPath 以匹配我的 XML?请建议我如何解决这个问题

最佳答案

尝试如下更改 xpath-->

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

关于c# - 使用数据集的 xml 到 TreeView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10603269/

相关文章:

c# - Imgur OAuth2 身份验证和上传 C#

c# - 最小起订量,严格与宽松的用法

c# - 如何在 ASP.Net 图表控件中标记每周图表

asp.net 表单例份验证将 .net 2 更改为 .net4

c# - 如何通过回发保存数据?

python - 如何混合pandas和beautifulsoup从xml文件目录中提取一些元素标签?

c# - C# 中的斐波那契、二进制或二项式堆?

c# - 如何翻转字符串?

java - XML 单元测试 - 打开 XML - 检查它是否有效 - 检查某些标签是否存在于 XML 中

xml - XSLT 中无用的 match=text()