簡體   English   中英

C#XML LINQ查詢

[英]C# XML linq query

嗨,我有以下XML:

<EPICORTLOG>
    <POS>
        <ClientId>WkStn.90.1</ClientId> 
        <Id>POS.90.20140819.251.8279</Id> 
        <StartTime>2014-08-25T05:12:34</StartTime> 
        <Store>90</Store> 
        <SysDate>2014-08-19T00:00:00</SysDate> 
        <TillNo>1</TillNo> 
        <Time>2014-08-25T05:12:34</Time> 
        <Tran>1093</Tran> 
        <WkStn>1</WkStn> 
        <WORKSTATION>
            <IsAutoLock>1</IsAutoLock> 
        </WORKSTATION>
        <TRADE>     
            <ITEM>
                <Class>102499</Class>  
                <Desc>NIKE RACER</Desc>   
                <FinalPrice>82.77</FinalPrice>   
                <Status>ACTV</Status> 
                <Style>EV0615</Style> 
                <Tag>1</Tag> 
            </ITEM>
        </TRADE>
    </POS>
</EPICORTLOG> 

實際的XML中有許多類似上面的POS節點。 我正在嘗試獲取ID = POS.90.20140819.251.8279的POS節點,然后從該特定節點獲取Item的詳細信息。 我寫了以下查詢:

XDocument xdoc = XDocument.Load(XMLFile);    
var item = from items in xdoc.Element("EPICORTLOG").Descendants("POS")
           where items.Attribute("Id").Value == strSelectedPOSID
           select new
           {
               desc=items.Element("ITEM").Attribute("Desc")
           };

但這對我沒有任何結果。 在這里strSelectedPOSID = POS.90.20140819.251.8279。 請讓我知道我要去哪里錯了。

IdDesc 不是 Attributes 他們是Elements所以你應該使用

var item = from items in xdoc.Descendants("POS")
           where (string)items.Element("Id") == strSelectedPOSID     
           select new
                  {
                      desc = (string)items.Element("ITEM").Element("Desc")
                  };

我終於得到了價值! 以下是我使用的:

 var item = from items in xdoc.Element("EPICORTLOG").Descendants("POS")
                   where (string)items.Element("Id") == strSelectedPOSID
                   select new
                   {
                       desc =   items.Element("TRADE").Element("ITEM").Element("Desc").Value.ToString()
                   };

感謝您的投入。

暫無
暫無

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

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