vb.net中的xpath如何迭代迭代

时间:2012-03-04 10:56:25

标签: xml vb.net xpath

这可能是一个愚蠢的问题,解决方案是我忽略的,但假设以下xml文件(如下)

我正在迭代信息/测试项目,然后想再次遍历test_properties,但我不知道如何。

属性永远不是固定计数,因此它可以有1个属性或200个 我需要type ='“属性和属性的值 到目前为止我用这个:

    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>

1 个答案:

答案 0 :(得分: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