XElement value in C#

前端 未结 3 1490
天涯浪人
天涯浪人 2021-02-14 08:31

How to get a value of XElement without getting child elements?

An example:



    someValue
            


        
3条回答
  •  南旧
    南旧 (楼主)
    2021-02-14 09:03

    I think what you want would be the first descendant node, so something like:

    var value = XElement.Descendents.First().Value;
    

    Where XElement is the element representing your element.

    You can specifically ask for the first text element (which is "somevalue"), so you could also do:

    var value = XElement.Descendents.OfType().First().Value;
    

提交回复
热议问题