0

How do I parse the following XML using LINQ?
I need to insert into a database table OrderNumber, ShipAddress, ShipCity, ShipState for each Order & OrderCancelled.
Then in a separate table I need to insert OrderId from the Returns/Amount section.

<!-- language: lang-xml -->
<?xml version="1.0" encoding="utf-8"?>
<OrdersReport Date="2012-08-01">
<Client>
<ClientId>1</ClientId>
  <Orders>
    <Order>
      <OrderNumber>1</OrderNumber>
      <ShipAddress>123 Main St.</ShipAddress>
      <ShipCity>MyCity</ShipCity>
      <ShipState>AZ</ShipState>
    </Order>
    <Order>
      <OrderNumber>2</OrderNumber>
      <ShipAddress>111 Main St.</ShipAddress>
      <ShipCity>OtherCity</ShipCity>
      <ShipState>AL</ShipState>
    </Order>
    <OrderCancelled>
      <OrderNumber>3</OrderNumber>
      <ShipAddress>111 Main St.</ShipAddress>
      <ShipCity>OtherCity</ShipCity>
      <ShipState>AL</ShipState>
    </OrderCancelled>
  </Orders>  
  <Returns>
    <Amount>
      <OrderId>2</OrderId>
      <OrderId>3</OrderId>
    </Amount>
  </Returns> 
</Client>
<Client>
<ClientId>2</ClientId>
<!-- Same Tree structure as Client 1 -->
</Client>
</OrdersReport>

Based on the responses I got, I have updated the question.

var doc = XDocument.Load(rootFolder + "Generic.xml");
    var query = doc.Descendants("Order")
                   .Concat(doc.Descendants("OrderCancelled"))
                   .Select(x => new
                   {
                       OrderNumber = (int)x.Element("OrderNumber"),
                       ShipAddress = (string)x.Element("ShipAddress"),
                       ShipCity = (string)x.Element("ShipCity"),
                       ShipState = (string)x.Element("ShipState")
                   });           

    var amount = doc.Descendants("Amount")                         
                  .Select(y => new
                  {
                      OrderId = (int)y.Element("OrderId")
                  });

    foreach (var o in query)
    {
        Console.WriteLine(o.OrderNumber + o.ShipAddress + o.ShipCity);
    }
    foreach (var r in amount)
    {
        Console.WriteLine(r.OrderId);
    }

The amount enumeration is only giving me the first OrderId, what am I doing incorrectly?

1
  • Colors showed up after posting :-) but not still not indenting. Commented Nov 2, 2012 at 22:39

2 Answers 2

1

It sounds like you just want all the descendant Order/OrderCancelled elements:

var doc = XDocument.Load("file.xml");
var query = doc.Descendants("Order")
               .Concat(doc.Descendants("OrderCancelled"))
               .Select(x => new {
                           OrderNumber = (int) x.Element("OrderNumber"),
                           ShipAddress = (string) x.Element("ShipAddress"),
                           ShipCity = (string) x.Element("ShipCity"),
                           ShipState = (string) x.Element("ShipState")
                       });

Then you can iterate over the results, which will be a sequence of the anonymous type, and do whatever you need to insert it into the database.

Sign up to request clarification or add additional context in comments.

2 Comments

So, I could do a similar select for the Returns?
@Picflight: Absolutely, although note that you've have to get each OrderId element within each Amount.
0

I think it is possible without concatenation:

var query = from o in xdoc.Descendants("Orders").Elements()
            select new 
            {
                OrderNumber = (int)o.Element("OrderNumber"),
                ShipAddress = (string)o.Element("ShipAddress"),
                ShipCity = (string)o.Element("ShipCity"),
                ShipState = (string)o.Element("ShipState")
            };

3 Comments

How does that satisfy "for each Order & OrderCancelled"?
@JonSkeet sorry, forgot to do Elements()
Right, that will do it - so long as there are never any other elements within Orders.

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.