0

My XML has multiple elements but when I try to parse them using LINQ, I just get one element. Something is wrong with my select statement, but I'm having a devil of a time understanding what is wrong. The Result should be a dictionary populated with the InvoiceUid and InvoiceNumber for all the (2) invoiceListItems

Here is my XML:

    <?xml version="1.0" encoding="utf-8"?>
    <invoiceListResponse>
      <invoiceList>
        <invoiceListItem>
          <invoiceUid>39165890</invoiceUid>
          <invoiceDate>2013-12-26</invoiceDate>
          <invoiceNumber>W10001888</invoiceNumber>
          <contactUid>8363070</contactUid>
        </invoiceListItem>
        <invoiceListItem>
          <invoiceUid>39149309</invoiceUid>
          <invoiceDate>2013-12-24</invoiceDate>
          <invoiceNumber>W100</invoiceNumber>
          <contactUid>8363070</contactUid>
        </invoiceListItem>
      </invoiceList>
    </invoiceListResponse>

and the meat of my code:

         Dim list As XmlDocument = proxy.Find(queries)

        Dim InvoiceList = XDocument.Parse(list.InnerXml)
        ' get all <InvoiceListItem> elements from the xdocumetn
        Dim InvoiceListItems = From invoiceListItem In InvoiceList...<invoiceList>
                                 Select invoiceListItem

        'go through each InvoiceListItem in InvoiceListItems
        For Each InvoiceListItem In InvoiceListItems
            Console.WriteLine("Uid is {0} and Invoice Number is {1}", InvoiceListItem...<invoiceUid>.Value, InvoiceListItem...<invoiceNumber>.Value)
            returnInvoiceList.Add(InvoiceListItem...<invoiceNumber>.Value, CType(InvoiceListItem...<invoiceUid>.Value, Integer))
        Next
        Return returnInvoiceList

2 Answers 2

1

The problem is that your select statement is selecting invoiceList elements (of which there is only one). This should do what you expected:

Dim InvoiceListItems = From invoiceListItem In InvoiceList...<invoiceListItem>
                       Select invoiceListItem
Sign up to request clarification or add additional context in comments.

Comments

0

ah finally got it working by adding .Elements here:

For Each InvoiceListItem In InvoiceListItems.Elements

Comments

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.