Linq to XML selecting a node bases on a attribute value

前端 未结 3 1186
情书的邮戳
情书的邮戳 2021-01-04 12:16

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:

<         


        
3条回答
  •  失恋的感觉
    2021-01-04 12:33

    With LINQ you can easily select just the nodes that have a specified attribute, like this:

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

提交回复
热议问题