How to get a value of XElement
without getting child elements?
An example:
<?xml version="1.0" ?>
<someNode>
someValue
<child>1</child>
<child>2</child>
</someNode>
If i use XElement.Value for <someNode>
I get "somevalue<child>1</child><child>2<child>"
string but I want to get only "somevalue" without "<child>1</child><child>2<child>"
substring.
You can do it slightly more simply than using
Descendants
- theNodes
method only returns the direct child nodes:Note that this will work even in the case where the child elements came before the text node, like this:
There is no direct way. You'll have to iterate and select. For instance:
I think what you want would be the first descendant node, so something like:
Where
XElement
is the element representing your<someNode>
element.You can specifically ask for the first text element (which is "somevalue"), so you could also do: