How to parse deeply nested using LINQ to XML

Posted by Picflight on Stack Overflow See other posts from Stack Overflow or by Picflight
Published on 2012-11-02T22:38:25Z Indexed on 2012/11/02 23:00 UTC
Read the original article Hit count: 457

Filed under:
|
|
|

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>

Not sure why the XML is not showing red and blue colors and not indenting properly. :-(

© Stack Overflow or respective owner

Related posts about c#

Related posts about Xml