6

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 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.

0

2 Answers 2

13

Try this:

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

For Each node As XmlElement In nodelist
   Console.Writeline(node("OrderID").InnerText)
Next
Sign up to request clarification or add additional context in comments.

Comments

2

The xPath expression I run that gets both orders is //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

5 Comments

Thanks for the response. If I replace ".//Orders/Order" with "//tns:Order" I get an exception. Can you explain what the //tns: path is?
Sorry forgot to attach the exception. It is Namespace Manager or XsltContext needed. This query has a prefix, variable, or user-defined function.
@user2516387 - tns is just the xml namespace alias that DonA used when creating a sample program. You can remove that.
if I use "//Order" it fails to process the for...next loop it ie. returns 0 nodes.
That's the path LiquidXML came up with. I thought it was odd, since I remembered using //Order in the past.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.