如何使用LINQ解析XML头和嵌套节

时间:2012-11-06 22:00:00

标签: c# .net xml linq

XElement documentRoot = XElement.Parse(@"<OrdersReport Date='2012-08-01'>
                <SettingHeader>
                  <SettingId>1</SettingId>
                </SettingHeader>
                  <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>222 Main St.</ShipAddress>
                        <ShipCity>CancelledCity</ShipCity>
                        <ShipState>AL</ShipState>
                      </OrderCancelled>
                    </Orders>
                    <Returns>
                      <Amount>
                        <OrderId>2</OrderId>
                        <OrderId>3</OrderId>
                        <OrderId>21</OrderId>       
                      </Amount>
                    </Returns>
                  </Client>
                  <Client>
                    <ClientId>2</ClientId>    
                  </Client>
                </OrdersReport>");

代码:

        var orders = documentRoot.Descendants("Order")
                       .Concat(documentRoot.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")
                       });

        foreach (var o in orders)
        {
            Console.WriteLine(o.OrderNumber + " " + o.ShipAddress + " " 
                + o.ShipCity);
        }

输出:

1 123 Main St. MyCity
2 111 Main St. OtherCity
3 222 Main St. CancelledCity
3 222 Main St. CancelledCity

如何附加 ClientId SettingId ,如下面的输出?

目标输出:

1 123 Main St. MyCity ClientId 1 SettingId 1
2 111 Main St. OtherCity ClientId 1 SettingId 1
...

2 个答案:

答案 0 :(得分:2)

试试这个:

var orders = doc.Descendants("Order").Select(x => new
    {
        OrderNumber = (int)x.Element("OrderNumber"),
        ShipAddress = (string)x.Element("ShipAddress"),
        ShipCity = (string)x.Element("ShipCity"),
        ShipState = (string)x.Element("ShipState"),
        ClientId = (int)x.Ancestors("Client").First().Element("ClientId"),
        SettingId = (int)doc.Descendants("SettingId").First(),
    });

答案 1 :(得分:0)

//pick the "settingId" first, it won't change in the XML
string settingId = 
    documentRoot.Descendants("SettingId").Single().Value;

var orders =
    //each client has its own clientId and Orders
    from c in documentRoot.Descendants("Client")
    let clientId = c.Element("ClientId").Value
    //search for the "Orders" tag and pick all their children 
    //this will include both "Order" and "OrderCancelled"
    from o in c.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"),
        ClientId = clientId,
        SettingId = settingId
    };