读取节点内的多个值

时间:2013-01-13 10:21:06

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

大家好我正在尝试使用linq.am读取一个xml文档时遇到问题,一个节点有两个同名的值,我怎么能得到这两个值。我只能使用此代码获得第一个。

XDocument doc = XDocument.Load("Orders.xml");
var data = from item in doc.Descendants("Order")
           select new
           {
               OrderID = item.Element("OrderID").Value,
               POnumber = item.Element("PurchaseOrderNumber").Value,
               OrderDate = item.Element("OrderPlacedDate").Value,
               PFirstName = item.Element("purchasingContact").Element("FirstName").Value,
               Pid= item.Element("CurrentOrderDetails").Element("ProductsId").Value
           };

我只能得到产品ID 34我怎么才能得到另一个呢?请帮忙

<Order>
    <OrderID>123</OrderID>
    <PurchaseOrderNumber>12</PurchaseOrderNumber>
    <OrderPlacedDate>12/23/2012</OrderPlacedDate>
    <PurchasingContact>
     <FirstName>test</FirstName>
    </PurchasingContact>        
    <CurrentOrderDetails>
       <ProductsId>34</ProductsId>
       <OrderQuantity>2</OrderQuantity>
       <ProductsId>35</ProductsId>
       <OrderQuantity>2</OrderQuantity>
    </CurrentOrderDetails>
</Order>
<Order>
    .......
</Order>

3 个答案:

答案 0 :(得分:1)

 XDocument doc = XDocument.Load("Orders.xml");
 var data = from item in doc.Descendants("Order")
            select new
            {
                OrderID = item.Element("OrderID").Value,
                POnumber = item.Element("PurchaseOrderNumber").Value,
                OrderDate = item.Element("OrderPlacedDate").Value,
                PFirstName = item.Element("purchasingContact").Element("FirstName").Value,
                Pids = item.Element("CurrentOrderDetails").Elements("ProductsId").Select(e => e.Value).ToList()
            };

但说实话 - 您应该考虑更改XML结构。 CurrentOrderDetails看起来应该更像:

<CurrentOrderDetails>
    <Product id="34" quantity="2" />
    <Product id="35" quantity="2" />
</CurrentOrderDetails>

<CurrentOrderDetails>
    <Product>
        <Id>34</Id>
        <Quantity>2</Quantity>
    </Product>
    <Product>
        <Id>35</Id>
        <Quantity>2</Quantity>
    </Product>
</CurrentOrderDetails>

这样可以更容易地获得产品ID及其数量。

答案 1 :(得分:1)

from o in doc.Descendants("Order")
let contact = o.Element("purchasingContact")
let details = o.Element("CurrentOrderDetails")
select new {
    OrderID = (int)o.Element("OrderID"),
    POnumber = (int)o.Element("PurchaseOrderNumber"),
    OrderDate = (DateTime)o.Element("OrderPlacedDate"),
    PFirstName = (string)contact.Element("FirstName"),
    Pids = details.Elements("ProductsId").Select(e => (int)e).ToList()
};

答案 2 :(得分:1)

如果你想在你的结构中将Pids作为一个数组,你可以拥有

Pids = item.Element("CurrentOrderDetails")
    .Elements("ProductsId")
    .Select(e => e.Value).ToArray()

如果您希望所有条目都作为一个单位列表,那么可以选择

var data = from item in doc.Descendants("Order")
           from product in item.Element("CurrentOrderDetails").Elements("ProductsId")
                     select new
                     {
                         OrderID = item.Element("OrderID").Value,
                         POnumber = item.Element("PurchaseOrderNumber").Value,
                         OrderDate = item.Element("OrderPlacedDate").Value,
                         PFirstName = item.Element("purchasingContact")
                             .Element("FirstName").Value,
                         Pid = product.Value
                      };

顺便说一下:XML模型应该在productsid周围有一个周围的元素,orderquantity明确地为订单行建模。这将更具表现力和直观性。