简体   繁体   中英

C# XML linq query

Hi I have the following 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> 

There are many POS nodes like above in the actual XML. I am trying to fetch the POS node with ID=POS.90.20140819.251.8279 and then the details of Item from that particular node. I have written the following query:

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

But it is not yielding any result for me. Here strSelectedPOSID=POS.90.20140819.251.8279. Please let me know where i am going wrong.

Id and Desc are not an Attributes . they are Elements so you should use

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

I got the value at last!! Following is what i used:

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

Thanks for the inputs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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