使用 VB.NET 遍历 XML 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17276580/
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
looping through XML file using VB.NET
提问by user2516387
I am having a problem processing an XMl file. I want to loop through (using VB.NET) the file and extract all the values of the OrderID element.
我在处理 XML 文件时遇到问题。我想遍历(使用 VB.NET)文件并提取 OrderID 元素的所有值。
<?xml version="1.0"?>
<ListOrdersResponse xmlns="https://xxx.xxxxxx.com/Orders/999uuu777">
<ListOrdersResult>
<NextToken>XXXXXXXXXX</NextToken>
<Orders>
<Order>
<ShipmentServiceLevelCategory>Standard</ShipmentServiceLevelCategory>
<OrderId>ooooooooo</OrderId>
</Order>
<Order>
<ShipmentServiceLevelCategory>Standard</ShipmentServiceLevelCategory>
<OrderId>ujuujujuj</OrderId>
</Order>
</Orders>
<CreatedBefore>2013-06-19T09:10:47Z</CreatedBefore>
</ListOrdersResult>
<ResponseMetadata>
<RequestId>8e34f7d9-3af7-4490-801b-cccc7777yu</RequestId>
</ResponseMetadata>
</ListOrdersResponse>
Here is the code I am trying but it does not loop through each order
这是我正在尝试的代码,但它不会遍历每个订单
Dim doc As New XmlDocument()
doc.Load(file)
Dim nodelist As XmlNodeList = doc.SelectNodes(".//Orders/Order")
For Each node As XmlElement In nodelist
console.writeline(node.SelectSingleNode("OrderID").InnerText)
Next
Any help would be gratefully appreciated.
任何帮助将不胜感激。
回答by SysDragon
Try this:
尝试这个:
doc.Load(file)
nodelist = doc.GetElementsByTagName("Order")
For Each node As XmlElement In nodelist
Console.Writeline(node("OrderID").InnerText)
Next
回答by OneFineDay
The xPath expression I run that gets both orders is //tns:Order
我运行的得到两个订单的 xPath 表达式是 //tns:Order
Dim doc As New XmlDocument()
doc.Load(file)
Dim nodelist As XmlNodeList = doc.SelectNodes("//tns:Order")
For Each node As XmlElement In nodelist
'2 exist
Next

