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

I want to xpath navigate to a node, then move to sibling mode, create a nodelist to write the contents to a listbox

我对VB还是很陌生,对于XML和xpath来说更是如此。我有一个listview框,该框传递一个变量,该变量用于标识xml文件中的节点(在变量下方的情况下,是id元素的文本值。

该变量选择一个节点,但是我需要一些其同级/子节点的内部文本。
我知道如何制作xmlnodelists并编写它们的内部文本没问题,但是我无法完成这项工作。这是我的xml示例(我没有写xml文件,因此无法更改它):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<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 "

这是我的代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
 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><center>[wp_ad_camp_2]</center></p><p> elements and have some sort of do statement and write them to
    '
to the IndForm.Listbox1

    IndForm.Show()
End Sub

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


最后的工作代码,谢谢JRRishe:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 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

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

1
2
3
4
5
6
7
 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