简体   繁体   中英

Obtaining childNodes from this xml

I am trying to extract the childnodes using XML.XMLNode as currently I am using the following code to get my value information.

Dim objXML As New Xml.XmlDocument
Dim colNode As Xml.XmlNode
Dim rowNode As Xml.XmlNode
Dim RespNode As Xml.XmlNode

For Each RespNode In objXML.ChildNodes
            For Each rowNode In RespNode.ChildNodes
                For Each colNode In rowNode.ChildNodes
testStr = colNode.Attributes.GetNamedItem("name").Value
Next
Next
Next

How can I get the fieldPasswordValue: 1234 and fieldPasswordValue: 4567 by modifying the code I'm currently using?

XML:

 <resp status="ok">
       <data id="41170">
        <field name="abc">xyz</field>
          <composite>
             <data id="51253">
                <field name="fieldPasswordValue"/>1234</field>
            </data>
             <data id="52356">
                <field name="fieldPasswordValue"/>4567</field>
           </data>
    </composite>
    </data>
    </resp>

First, your xml is wrong. You are closing the field twice by doing

<field name="fieldPasswordValue"/>1234</field>

It should be

<field name="fieldPasswordValue">1234</field>

To get the value, you can search the xml using xpath.

For Each node As Xml.XmlNode In objXML.SelectNodes("//field[@name='fieldPasswordValue']")
     Console.WriteLine(node.InnerText)
Next

I suggest youread up on the syntax of xpath to get exactly what you are looing for. You could even remove your 3 for loops in your sample.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM