XElement value in C#

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

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

An example:



    someValue
            


        
3条回答
  •  星月不相逢
    2021-02-14 08:53

    There is no direct way. You'll have to iterate and select. For instance:

    var doc = XDocument.Parse(
        @"somevalue12");
    var textNodes = from node in doc.DescendantNodes()
                    where node is XText
                    select (XText)node;
    foreach (var textNode in textNodes)
    {
        Console.WriteLine(textNode.Value);
    }
    

提交回复
热议问题