我有一个XML文件,它看起来像这样选择给定节点,LINQ的所有属性和它的价值为xml
<?xml version="1.0" encoding="utf-8"?>
<Content>
<Field Title="XYZ">
<Description>Numeric</Description>
<Comment>12345</Comment>
</Field>
<Field Title="ABC">
<Description>Alphabetic</Description>
<Comment>QWERTY</Comment>
</Field>
<Field Title="XYZ">
<Description>Alphabetic</Description>
<Comment>QWERTY</Comment>
</Field>
</Content>
我想提取的<Description>
节点值和<Comment>
,根据属性Title
自带Value='XYZ'
我想是这样的使用LINQ to XML,但是没有得到预期的结果
string Xmlpath = @"d:\Test.xml";
XDocument mappings = XDocument.Load(Xmlpath);
var Data = from elm in mappings.Descendants("Field")
where (string)elm.Attribute("Title") == "XYZ"
select elm;
有人可以帮助我吗?
注意,'.'是C#? 6.0和OP用C#4.0标记问题。 – Tim
OP使用的代码对于NRE也是安全的:'(string)elm.Attribute(“Title”)==“XYZ”' – har07
@Jithin j - 是否解决了您的问题? –