使用VB.NET循环遍历XML文件

时间:2013-06-24 13:23:00

标签: xml vb.net parsing

我在处理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>

这是我正在尝试的代码,但它不会遍历每个订单

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

感谢任何帮助。

2 个答案:

答案 0 :(得分:7)

试试这个:

doc.Load(file)
nodelist = doc.GetElementsByTagName("Order")

For Each node As XmlElement In nodelist
   Console.Writeline(node("OrderID").InnerText)
Next

答案 1 :(得分:0)

我运行的获取两个订单的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