XDocument get XML element by the value of its name attribute

前端 未结 2 1672
暖寄归人
暖寄归人 2021-01-01 16:20

I have an XML result like this


  
    0
    

        
2条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-01 16:40

    Does this solve your problem:

    var test = from c in xml.Descendants("doc")
               select new 
               {
                   firstname = c.Elements("str").First(element => element.Attribute("name").Value == "ContaFirstname"),
                   surnmane = c.Elements("str").First(element => element.Attribute("name").Value == "ContaSurname")
               }; 
    

    or, if you want the values (instead of XElement:

    var test = from c in xml.Descendants("doc")
               select new 
               {
                   firstname = c.Elements("str").First(element => element.Attribute("name").Value == "ContaFirstname").Value,
                   surnmane = c.Elements("str").First(element => element.Attribute("name").Value == "ContaSurname").Value
               }; 
    

提交回复
热议问题