2012-03-04 31 views
0

遍歷這可能是一個愚蠢的問題和解決的辦法是什麼我忽略了,但假設下面的XML文件(如下圖)的XPath在vb.net如何迭代

我通過info /測試項目iteratiing然後想通過test_properties再次迭代,但我不知道如何。

性質是從未固定的數,因此它可以有1個屬性或200 我需要的類型='」屬性和屬性 我用這個到目前爲止的值:

Dim Xml_Document As XPathDocument = New XPathDocument("e:\test.xml") 
      Dim Navigator As XPathNavigator = Xml_Document.CreateNavigator() 
      Dim ns As XmlNamespaceManager = New XmlNamespaceManager(Navigator.NameTable) 
      Dim NodeIterator As XPathNodeIterator = Navigator.Select("/info/test") 


      Try 
       While NodeIterator.MoveNext() 
        Dim sanction_id As Integer = CInt(NodeIterator.Current.GetAttribute("id", ns.DefaultNamespace)) 
        Dim clone As XPathNavigator = NodeIterator.Current.Clone() 

        clone.MoveToFirstChild() 
        MsgBox(clone.Value) 
        clone.MoveToNext() 
        MsgBox(clone.Value) 
        clone.MoveToNext() 
        MsgBox(clone.Value) 
        clone.MoveToNext() 
        MsgBox(clone.Value) 
        clone.MoveToNext() 
        MsgBox(clone.Value) 
        clone.MoveToNext() 
        MsgBox(clone.Value) 
        clone.MoveToNext() 
'''''''' 
' here i want to iterate through test_properties for each property 
''''''' 

       End While 
      Catch ex As Exception 
       Console.WriteLine(ex.StackTrace) 
      End Try 

和在XML

<?xml version="1.0" encoding="UTF-8"?> 
<info> 
    <test id="1"> 
     <test_source>ONLINE</test_source> 
     <test_type><![CDATA[P]]></test_type> 
     <test_date><![CDATA[2012-02-23]]></test_date> 
     <test_programme><![CDATA[UK]]></test_programme> 
     <test_remark><![CDATA[Nice guy]]></test_remark> 
     <test_key>123456789</test_key> 
     <test_properties> 
      <property type="FIRSTNAME"><![CDATA[Robert]]></property> 
      <property type="MIDDLENAME"><![CDATA[]]></property> 
      <property type="LASTNAME"><![CDATA[Johnson]]></property> 
      <property type="GENDER"><![CDATA[M]]></property> 
      <property type="BIRTHDATE"><![CDATA[1900-01-01]]></property> 
     </test_properties> 
    </test> 
    <test id="2"> 
     <test_source>ONLINE</test_source> 
     <test_type><![CDATA[P]]></test_type> 
     <test_date><![CDATA[2012-02-23]]></test_date> 
     <test_programme><![CDATA[UK]]></test_programme> 
     <test_remark><![CDATA[Nice girl]]></test_remark> 
     <test_key>123456789</test_key> 
     <test_properties> 
      <property type="FIRSTNAME"><![CDATA[Roberta]]></property> 
      <property type="MIDDLENAME"><![CDATA[]]></property> 
      <property type="LASTNAME"><![CDATA[Johnsons]]></property> 
      <property type="GENDER"><![CDATA[M]]></property> 
      <property type="BIRTHDATE"><![CDATA[1900-01-01]]></property> 
     </test_properties> 
    </test> 
</info> 

回答

0
For Each test As XPathNavigator In Xml_Document.CreateNavigator().Select("/info/test") 
    For Each prop As XPathNavigator In test.Select("test_properties/property") 
    Console.WriteLine("{0}: {1}", prop.GetAttribute("type", null), prop.Value); 
    Next 
Next 
+0

完美...這工作就像一個魅力...我以前不知道我應該爲EAC做一個在導航...謝謝! – renevdkooi 2012-03-04 11:27:05