1

次の構造の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>
        <Id>1601</Id>
        <Industry>Oil &amp; Gas Operations</Industry>
        <ParentId>1689</ParentId>
    </row>
    <row>
</table>

この XML ファイルを使用してツリービューを作成したいと考えています。私は次のコードを書いています

   ' Load a TreeView control from an XML file.
    Private Sub LoadTreeViewFromXmlFile(ByVal file_name As String, ByVal trv As TreeView)
        ' Load the XML document.
        Dim xml_doc As New XmlDocument
        xml_doc.Load(file_name)

        ' Add the root node's children to the TreeView.
        trv.Nodes.Clear()
        trv.Nodes.Add(New TreeNode(xml_doc.DocumentElement.Name))
        AddTreeViewChildNodes(trv.Nodes, xml_doc.DocumentElement)
    End Sub

    ' Add the children of this XML node 
    ' to this child nodes collection.
    Private Sub AddTreeViewChildNodes(ByVal parent_nodes As TreeNodeCollection, ByVal xml_node As XmlNode)
        For Each child_node As XmlNode In xml_node.ChildNodes
            ' Make the new TreeView node.
            Dim new_node As TreeNode = New TreeNode(child_node.Item("Industry").InnerText, child_node.Item("Id").InnerText)

            parent_nodes.Add(new_node)

        Next child_node
    End Sub

しかし、次のようなツリービューを作成します:

->table
->Associations
->Fortune 100

このような親要素としてテーブルが必要な場合

->table
  ->Associations
  ->Fortune 100

テーブルノードをクリックすると、すべてのツリーが折りたたまれたり展開されたりします。どうすれば修正できるか提案してください

4

1 に答える 1

0

問題は、ツリービューコントロール自体をAddTreeViewChildNodesメソッドに渡しているため、同じトップレベルにすべてのノードが追加されていることです。作成したルートノードをメソッドに渡して、その下に子を追加する必要があります。

変化する:

    trv.Nodes.Add(New TreeNode(xml_doc.DocumentElement.Name))
    AddTreeViewChildNodes(trv.Nodes, xml_doc.DocumentElement)

に:

    Dim rootNode As TreeNode = New TreeNode(xml_doc.DocumentElement.Name)
    trv.Nodes.Add(rootNode)
    AddTreeViewChildNodes(rootNode, xml_doc.DocumentElement)
于 2012-05-15T18:29:00.337 に答える