我想用xpath导航到一个节点,然后转到同级模式,创建一个nodeist将内容写入一个列表框



我对VB非常陌生,对XML和xpath更是如此。我有一个listview框,它传递了一个变量,用于标识xml文件中的节点(在这种情况下,变量下面是id元素的文本值)。

变量选择一个节点,但我需要它的一些兄弟/子节点的innertext。我知道如何制作xmlnodelist并编写它们的内部文本,但我不会这样做。这里是我的xml的一个例子(我没有写xml文件,因此不能改变它):

<file>
  <outcomes>
   <outcome>
    <id>CPK</id>
    <id_text>description</outcome_text>
    <indicators>
        <indicator>
            <p>the text i want to write to listbox</p>
        </indicator>
        <indicator>
            <p>more text I want to write</p>
        </indicator>
    </indicators>
   </outcome>
  <outcome>
  <outcome>
    <id>CPK</id>
    <id_text>description</id_text>
    <indicators>
        <indicator>
            <p>the text i want to write to listbox</p>
        </indicator>
        <indicator>
            <p>more text I want to write</p>
        </indicator>
    </indicators>
   </outcome>
  <outcome>
 </outcomes>    
</file>

假设传递的变量是"CPK"

下面是我的代码:
 Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles Button1.Click
    'get variable to identify node
    Dim id As String
    Dim out As String
    id = Me.outListView.SelectedItems(0).Text
    out = Me.outListView.SelectedItems(0).SubItems(1).Text
    IndForm.SelOutBox.Text = id & "  " & out
    'start xpath navigation
    Dim document As XPathDocument = New XPathDocument("C:Temp" & sb & "_education_" & gr & ".XML")
    Dim navigator As XPathNavigator = document.CreateNavigator()

    'select the node with with variable, by text value
    navigator.SelectSingleNode("/files/outcomes/goal_section/outcome/id[@text='" & id & "']")
    'move to the needed node
    navigator.MoveToNext() 'should be at /outcome/id_text
    navigator.MoveToNext() 'should be at /outcome/indicators
    navigator.MoveToFirstChild() 'should be at /outcome/indicators/indicator
    navigator.MoveToFirstChild() 'should be at /outcome/indicators/indicator/p
    'now what? I want to loop through the <p> elements and have some sort of do statement and write them to 
    'to the IndForm.Listbox1
    IndForm.Show()
End Sub  

实际文件中每个ID有许多"p"元素。任何帮助(我相信有一个更简单的方法来做到这一点。

如果您只想要与您拥有的ID匹配的p s的值(我将使用半伪代码,因为我不太了解VB语法):

 Dim pNodes as XPathNodeIterator = navigator.Select(
                 "/files/outcomes/outcome[id = '" & id & 
                 "']/indicators/indicator/p")
 While pNodes.MoveNext
    ' pNodes.Current.Value is the current value
 Loop

最终工作代码,感谢JRRishe:

 Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles Button1.Click
    'get variable to identify node
    Dim id As String
    Dim out As String
    id = Me.outListView.SelectedItems(0).Text
    out = Me.outListView.SelectedItems(0).SubItems(1).Text
    IndForm.SelOutBox.Text = id & "  " & out
    'start xpath navigation
    Dim doc As New Xml.XmlDocument()
    doc.load("C:Temp" & sb & "_education_" & gr & ".XML")
    'select the node with with variable, by text value
    Dim idList As Xml.XmlNodeList = doc.SelectNodes("/curriculum/outcomes/outcome[id = '" & id & "']/indicators/indicator/p")
    For Each p As Xml.XmlNode In idList
        IndForm.curIndBox.Items.Add(p.InnerText)
    Next
    IndForm.Show()
End Sub

最新更新