C#XML linq查询
嗨,我有以下XML:C#XML linq查询,c#,xml,linq,C#,Xml,Linq,嗨,我有以下XML: <EPICORTLOG> <POS> <ClientId>WkStn.90.1</ClientId> <Id>POS.90.20140819.251.8279</Id> <StartTime>2014-08-25T05:12:34</StartTime> <Store>90</Stor
<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>
但这并没有给我带来任何结果。此处strSelectedPOSID=POS.90.20140819.251.8279。请告诉我哪里出了问题。
Id
和Desc
是而不是属性。它们是元素
,因此您应该使用
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()
};
感谢您的输入。当我尝试在Quick Watch中计算表达式时,它仍在为对象引用未设置为对象错误的实例。请帮助。我对其进行了如下修改:var item=来自xdoc.Element(“EPICORTLOG”)中的items。子体(“POS”)其中(string)items.Element(“Id”)==strSelectedPOSID选择新的{desc desc=items.Element(“TRADE”).Element(“item”).Attribute(“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()
};