读书人

指教一个linq to xml的查询语句,该怎么

发布时间: 2012-02-28 13:06:36 作者: rapoo

指教一个linq to xml的查询语句

XML code
<?xml version="1.0" encoding="utf-8" ?><table>  <record id="1" name="aa" age="10"/>  <record id="2" name="bb" age="20"/>  <record id="3" name="cc" age="30"/>  <record id="4" name="dd" age="40"/>  <record id="5" name="ee" age="50"/></table>

有这样一个xml文件,我想查询出指定属性值的某一个元素出来,语句应该怎么写,就比如想要查询出id=3的record元素出来
我这样写,但是提示是错误的
XElement elements = from A in xml.Document.Root.Descendants("record") where A.Attributes("id").Equals("3") select A;

[解决办法]
C# code
var xdoc = XDocument.Load("xml文件路径");var record = xdoc.Descendants("record").First(x => x.Attribute("id").Value == "3");
[解决办法]

XML code
<?xml version="1.0" encoding="utf-8" ?><table>  <record id="1" name="aa" age="10"/>  <record id="2" name="bb" age="20"/>  <record id="3" name="cc" age="30"/>  <record id="4" name="dd" age="40"/>  <record id="5" name="ee" age="50"/></table> 

读书人网 >.NET

热点推荐