0

私は 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" 要素があります。任意のヘルプ(これを行うためのはるかに簡単な方法があると確信しています.

4

2 に答える 2

0

最終的な作業コード、ありがとう JRRish:

 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
于 2013-03-11T22:00:05.997 に答える
0

持っている ID に一致する s の値だけが必要な場合p(私は 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
于 2013-03-11T06:49:02.137 に答える