简体   繁体   中英

Retrieve specific values from specific nodes - VB.NET XML HTTP GET

I have a program I am writing that calls a Feed using HTTP GET that returns XML.

I have written code to be able to read each Element and their Values but I only wish to retrieve specific Element Values to specific variables.

I could use nested If statements to check for the 'reader.value' and then put it to the relevant variable but feel this seems cumbersome and would like to have a better understanding.

This is my current code:

Const URLString As String = "https://geturl"
    Dim reader As XmlTextReader = New XmlTextReader(URLString)

    Do While (reader.Read())
        Select Case reader.NodeType
            Case XmlNodeType.Element 'Display beginning of element.
                Console.Write("<" + reader.Name)
                If reader.HasAttributes Then 'If attributes exist
                    While reader.MoveToNextAttribute()
                        'Display attribute name and value.
                        Console.Write(" {0}='{1}'", reader.Name, reader.Value)
                        MsgBox(reader.Value)
                    End While
                End If
                Console.WriteLine(">")
            Case XmlNodeType.Text 'Display the text in each element.
                Console.WriteLine(reader.Value)
            Case XmlNodeType.EndElement 'Display end of element.
                Console.Write("</" + reader.Name)
                Console.WriteLine(">")
        End Select
    Loop

This is the information that is returned:

<result id="4198608" generated="1399463340" mode="live" account_id="428">
<vrm>GY08OJB</vrm>
<make>AUDI</make>
<model>A6 SE TDI</model>
<colour>BLACK</colour>
<body>ESTATE</body>
<doors>5 DOORS</doors>
<engine_size>1968</engine_size>
<fuel>HEAVY OIL</fuel>
</result>

I wish to return each individual node to an individual record so I can return it to some textboxes.

Cheers :/

Since you use VB.Net you can go the easy route and use XML literals:

' Load your xml into an XElement '
Dim xml = <result id="4198608" generated="1399463340" mode="live" account_id="428">
                <vrm>GY08OJB</vrm>
                <make>AUDI</make>
                <model>A6 SE TDI</model>
                <colour>BLACK</colour>
                <body>ESTATE</body>
                <doors>5 DOORS</doors>
                <engine_size>1968</engine_size>
                <fuel>HEAVY OIL</fuel>
            </result>


Dim vrm = xml.<vrm>.Value      ' is now GY08OJB   '
Dim make = xml.<make>.Value    ' is now AUDI      '
Dim model = xml.<model>.Value  ' is now A6 SE TDI '

I used the following for anyone that is interested:

   Dim strReg
    Dim doc As XmlDocument = New XmlDocument()

doc.Load(myURL)

        doc.SelectSingleNode("<root>/<node>").InnerText

Cheers.

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