簡體   English   中英

構造正確的linq-to-xml查詢時遇到問題

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

我是linq的新手,我無法編寫一個查詢來提取我要查找的數據。 xml文件有訂單,每個訂單都有采購訂單信息和產品,每個產品都有自己的元素和后代。 我需要將所有訂單及其后代查詢到一個集合中,但是由於某種原因,linq查詢語法對我來說是非常違反直覺的。

這是我的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>

我的對象看起來像這樣:

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

因此,我一直在一天中的大部分時間里進行查詢,但似乎並不能解決問題。 到目前為止,這是我得到的:

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

//執行將集合放入列表中的代碼

但是,當我這樣做時,我似乎最終會收到“對象引用未設置為對象實例”的信息。 子查詢錯誤。 當我注釋掉Linq_proxy和Linq_serviceCode行時,我得到了結果,但沒有得到正確的結果。 當我遍歷列表並獲取單個訂單,然后查看該訂單的產品清單時,計數就是該文件的產品總數(4),而不是該訂單的2。 有什么想法嗎?

我只是運行它,它運行良好:

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

注意,您可以將集合直接投影到您類型的對象中,因此以后不必再有代碼來創建訂單集合。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM