[英]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. 我有一个正在编写的程序,该程序使用返回XML的HTTP GET调用Feed。
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. 我可以使用嵌套的If语句来检查“ reader.value”,然后将其放入相关变量,但是感觉这似乎很麻烦,并且希望有一个更好的理解。
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: 由于使用的是VB.Net,因此可以轻松进行操作并使用XML文字:
' 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. 干杯。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.