Given the code:
var doc = new XmlDocument();
doc.LoadXml(@"<a>
<b>test
<c>test2</c>
</b>
</a>");
var node = doc.SelectNodes("/a/b")[0];
I want to then extract the 'text' value of node b
- in this case "test", without retrieving all text elements from all child nodes (as .innerText
does)
I find myself resorting to this code
var elementText = node.ChildNodes.Cast<XmlNode>().First(a => a.NodeType == XmlNodeType.Text).Value;
As unfortunately node.Value
does something else in this case
is there a neater/inbuilt way without resorting to linq casting? that doesnt involve me doing something like;
foreach (var childNode in node.ChildNodes)
if (childNode.NodeType==XmlNodeType.Text)
...