我有一个xml文件,它返回一组属性值唯一的元素。这提出了一个问题,因为我无法通过名称选择节点:
<doc>
<float name="score">1.2873721</float>
<arr name="2_category">
<long>3021</long>
</arr>
<arr name="ATR_FamilyName">
<str>Some Cookbook </str>
</arr>
<arr name="ATR_IsFamily">
<str>0</str>
</arr>
<arr name="ATR_SellPrice">
<str>49.95</str>
</arr>
<arr name="ATR_VendorId">
<str>ABC</str>
</arr>
<arr name="ATR_VendorName">
<str>WROX</str>
</arr>
</doc>
我正在使用linq来填充“Product”类。我可以按位置选择元素,但如果节点不存在,这就成了问题。有没有办法根据其属性的值选择节点?在下面的示例中,如果@name属性=“ATR_FamilyName”,我可以获取arr节点吗?在xpath中它将是:
doc/arr[@name = 'ATR_FamilyName']/str
这是我的linq to xml查询:
var query = from rt in results
where (String)rt.Descendants().ElementAt(5).Element("str").Value == "0"
select new Product.Product
{
FamilyName = (String)rt.Descendants().ElementAt(3).Value
// doc/arr[@name = 'ATR_FamilyName']/str - select Family Name is arr/@name 'ATR_FamilyName'
MorePropertiestoset....
};
答案 0 :(得分:21)
与AS-CII的答案类似,但不使用查询表达式(外部表达式除外),并使用XAttribute
的强制转换,并在匿名类型中选择str
元素值:< / p>
select new Product.Product
{
FamilyName = rt.Descendants("arr")
.Where(x => (string) x.Attribute("name") == "ATR_FamilyName")
.Select(x => (string) x.Element("str"))
.FirstOrDefault(),
MorePropertiesToSet....
};
请注意,对Attribute("name")
调用的结果使用强制转换意味着如果不具有该属性的任何元素,则强制转换将导致null引用(不等于字符串文字)。如果您使用Value
属性,则会收到异常。有时异常可能更好 - 如果这表明数据从根本上被打破并且您想要了解它而不是仅仅与该值不匹配。
(对于XElement
到string
的演员也是如此。)
答案 1 :(得分:6)
使用LINQ,您可以轻松选择具有指定属性的节点,如下所示:
var query = from node in results.Descendants("arr") // I believe you could use results.Elements("arr") here
where node.Attribute("name").Value == "ATR_FamilyName"
select new Product
{
FamilyName = node.Element("str").Value
};
答案 2 :(得分:2)
像这样使用XElement
:
from rt in results.descendants("<node name>")
where rt.attribute(attribute name).value == "specified value"
select rt
很抱歉从手机输入