如何使用LINQ to XML深度嵌套

时间:2012-11-02 22:38:25

标签: c# xml linq linq-to-xml

如何使用LINQ解析以下XML?
我需要为每个订单&amp ;;插入数据库表 OrderNumber,ShipAddress,ShipCity,ShipState 。的 OrderCancelled 即可。
然后在另一个表中,我需要从退货/金额部分插入 OrderId

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

根据我得到的回复,我已经更新了问题。

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);
    }

金额枚举只给我第一个OrderId,我做错了什么?

2 个答案:

答案 0 :(得分:1)

听起来你只想要所有后代的Order / OrderCancelled元素:

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")
                       });

然后你可以迭代结果,这将是一个匿名类型的序列,并做你需要的任何东西插入数据库。

答案 1 :(得分:0)

我认为没有连接就有可能:

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")
            };