繁体   English   中英

Linq 到 XML 根据属性值选择节点

[英]Linq to XML selecting a node bases on a attribute value

我有一个 xml 文件,它返回一组由属性值唯一的元素。 这提出了一个问题,因为我不能通过名称 select 节点:

<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 来填充“产品”class。我能够通过 position 对元素进行 select,但是如果节点不存在,这将成为一个问题。 有没有办法根据节点的属性值 select 一个节点? 在下面的示例中,如果 @name 属性 = "ATR_FamilyName",我可以获取 arr 节点吗? 在 xpath 中它将是:

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

这是我的 linq 到 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....                              
        };

与AS-CII的答案一样,但不使用查询表达式(外部表达式除外),并使用XAttribute ,并在匿名类型中选择str元素值:

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属性,则会出现异常。 有时异常可能更好 - 如果这表明数据从根本上被打破并且您想要找到它而不是仅仅不匹配该值。

(将XElementstring也是如此。)

使用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
            };

像这样使用XElement

from rt in results.descendants("<node name>")
where rt.attribute(attribute name).value == "specified value"
select rt

很抱歉从手机打字

由于rt是您各自的 XElement 根,您也可以像这样使用扩展方法XPathSelectElement

rt.XPathSelectElement("descendant::arr[@name = 'ATR_FamilyName']/str")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM