从特定节点检索特定值 - VB.NET XML HTTP GET
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23517562/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Retrieve specific values from specific nodes - VB.NET XML HTTP GET
提问by Lynchie
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 :/
干杯:/
回答by sloth
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 '
回答by Lynchie
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.
干杯。

