简体   繁体   English

构造正确的linq-to-xml查询时遇到问题

[英]Having trouble constructing the proper linq-to-xml query

I'm new to linq and I'm having trouble writing a query that pulls back the data I'm looking for. 我是linq的新手,我无法编写一个查询来提取我要查找的数据。 The xml file has orders, each order has purchase order info and products, each prouct has its own elements and descendants. xml文件有订单,每个订单都有采购订单信息和产品,每个产品都有自己的元素和后代。 I need to query all the orders and their descendants into one colletion but for some reason the linq query syntax is very counter-intuitive to me. 我需要将所有订单及其后代查询到一个集合中,但是由于某种原因,linq查询语法对我来说是非常违反直觉的。

Here's a truncated sample of my xml 这是我的xml的截短的示例

<fulfillment>
   <orders>
      <order>
         <isbulk>true</isbulk> 
         <purchaseorder>
            <id>Acme Inustries</id> 
            <quantity>15</quantity> 
         </purchaseorder>
         <items>
            <item>
               <prods>
                  <prod>
                     <seq>1</seq> 
                     <issuetype>NEW</issuetype> 
                     <loop>
                        <proxy>xyz123</proxy> 
                        <servicecode>55</servicecode> 
                     </loop>
                  </prod>
                  <prod>
                     <seq>2</seq> 
                     <issuetype>NEW</issuetype> 
                     <loop>
                        <proxy>abc987</proxy> 
                        <servicecode>121</servicecode> 
                     </loop>
                  </prod>
               </prods>
            </item>
         </items>
      </order>
      <order>
         <isbulk>true</isbulk> 
         <purchaseorder>
            <id>ABC Co</id> 
            <quantity>10</quantity> 
         </purchaseorder>
         <items>
            <item>
               <prods>
                  <prod>
                     <seq>1</seq> 
                     <issuetype>NEW</issuetype> 
                     <loop>
                        <proxy>xyz456</proxy> 
                        <servicecode>998</servicecode> 
                     </loop>
                  </prod>
                  <prod>
                     <seq>2</seq> 
                     <issuetype>NEW</issuetype> 
                     <loop>
                        <proxy>abc654</proxy> 
                        <servicecode>664</servicecode> 
                     </loop>
                  </prod>
               </prods>
            </item>
         </items>
      </order>
   </orders>
</fulfillment>

My objects look a bit like this: 我的对象看起来像这样:

public class order
{
    public bool IsBulk { get; set; }

    public PurchaseOrder PurchaseOrder = new PurchaseOrder();
    public List<prod> ListOfProds = new List<prod>();
}

public class prod
{
    public string Seq { get; set; }
    public string IssueType { get; set; }

    public string Proxy { get; set; }
    public string ServiceCode { get; set; }
}

public class PurchaseOrder
{
    public string ID { get; set; }
    public string Quantity { get; set; }
}

So I've been working on a query for the best part of a day and just can't seem to get it right. 因此,我一直在一天中的大部分时间里进行查询,但似乎并不能解决问题。 Here's what I've got so far: 到目前为止,这是我得到的:

List<order> orderlist = new List<order>();
XDocument xmlDoc = XDocument.Load(FilePath);

var list = (from myOrder in xmlDoc.Descendants("order")             
select new       
{
   linq_orderIsBulk = Convert.ToBoolean(myOrder.Element("isbulk").Value),

   linq_purchaseOrderID = myOrder.Element("purchaseorder").Element("id").Value,
   linq_purchaseOrderQuantity = myOrder.Element("purchaseorder").Element("quantity").Value,

   prodlist = myOrder.Element("items").Element("item").Element("prods").Elements("prod").Select(e => new
   {                
      Linq_seq = e.Element("seq").Value,
      Linq_IssueType = e.Element("issuetype").Value,

      Linq_proxy = e.Element("loop").Element("proxy").Value,
      Linq_serviceCode = e.Element("loop").Element("servicecode").Value
   }).ToList()
});

//do code to put the collection in list into List orderlist //执行将集合放入列表中的代码

But when I do this, I seem to end up getting an "Object reference not set to an instance of an object." 但是,当我这样做时,我似乎最终会收到“对象引用未设置为对象实例”的信息。 error on the subquery. 子查询错误。 When I comment out the Linq_proxy and Linq_serviceCode lines, I get results but not the right ones. 当我注释掉Linq_proxy和Linq_serviceCode行时,我得到了结果,但没有得到正确的结果。 When I loop through list and grab a single order then look at the prodlist for that order, the count is the total amount of prods for that file (4) instead of the 2 for that order. 当我遍历列表并获取单个订单,然后查看该订单的产品清单时,计数就是该文件的产品总数(4),而不是该订单的2。 Any thoughts? 有什么想法吗?

I just ran this, and it works fine: 我只是运行它,它运行良好:

var orders = new List<order>(
    from myOrder in xmlDoc.Descendants("order")
    let purchaseOrder = myOrder.Element("purchaseorder")
    select new order {
        IsBulk = Convert.ToBoolean(myOrder.Element("isbulk").Value),
        PurchaseOrder = new PurchaseOrder {
            ID = purchaseOrder.Element("id").Value,
            Quantity = purchaseOrder.Element("quantity").Value
        },
        ListOfProds = new List<prod>(
            from product in myOrder.Descendants("prod")
            let loop = product.Element("loop")
            select new prod
            {                
                Seq = product.Element("seq").Value,
                IssueType = product.Element("issuetype").Value,
                Proxy = loop.Element("proxy").Value,
                ServiceCode = loop.Element("servicecode").Value
            }
        )
    }
);

Notice that you can project a collection directly into objects of your type, so you don't have to have code later on to create your orders collection. 注意,您可以将集合直接投影到您类型的对象中,因此以后不必再有代码来创建订单集合。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM