Linq to XML基于属性值选择节点 [英] Linq to XML selecting a node bases on a attribute value

查看:80
本文介绍了Linq to XML基于属性值选择节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个xml文件,该文件返回一组通过属性值唯一的元素.这带来了一个问题,因为我无法通过其名称选择节点:

I have an xml file that returns a set of elements that are unique by a attribute value. This presents a problem, as I can not select a node by its name:

<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填充产品"类.我可以按位置选择元素,但是如果该节点不存在,这将成为一个问题.有没有一种方法可以根据其属性值选择节点?在下面的示例中,如果@name属性="ATR_FamilyName",我可以获取arr节点吗?在xpath中应该是:

I am using linq to populate a "Product" class. I am able to select the elements by position, however this becomes a problem if the node doesn't exist. Is there a way to select a node based on the value of its attribute? In the below example, can I get the arr node if the @name attribute = "ATR_FamilyName"? In xpath it would be:

doc/arr[@name = 'ATR_FamilyName']/str

这是我的linq to xml查询:

here is my linq to xml query:

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....                              
              };   

推荐答案

类似于AS-CII的答案,但不使用查询表达式(外部表达式除外),并且将其强制转换为XAttribute,然后选择str匿名类型内的元素值:

Like AS-CII's answer, but without using a query expression (except for the outer one), and with the cast for XAttribute, and selecting the str element value inside an anonymous type:

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")的调用结果使用强制类型转换表示,如果有任何具有属性的元素,则强制类型转换将导致空引用(这不等于字符串文字).如果使用Value属性,则会出现异常.有时,例外可能会更好-如果这表示数据从根本上被破坏了,而您想了解它而不是不匹配该值.

Note that the use of a cast for the result of the call to Attribute("name") means that if there are any elements which don't have the attribute, the cast will result in a null reference (which isn't equal to the string literal). If you use the Value property, you'll get an exception. Sometimes an exception may be better - if that indicates that the data is fundamentally broken and you want to find out about it rather than just not match the value.

(XElementstring的转换也是如此.)

(The same is true for the cast of the XElement to string.)

这篇关于Linq to XML基于属性值选择节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆