私は VB に非常に慣れていませんが、XML と xpath についてはなおさらです。xml ファイル内のノードを識別するために必要な変数を渡すリストビュー ボックスがあります (以下の場合、変数は id 要素のテキスト値です。
変数はノードを選択しますが、その兄弟/子ノードのいくつかの内部テキストが必要です。xmlnodelists を作成し、それらの内部テキストを問題なく書き込む方法は知っていますが、これを機能させることはできません。これが私の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" 要素があります。任意のヘルプ(これを行うためのはるかに簡単な方法があると確信しています.