Suppose I have some XmlElement
; let's call it element
. If I want to get all of the child nodes of that element, I can call element.ChildNodes
. If I want the text of the element and all its children, then I can go with element.InnerText
.
However, suppose I have some XML that looks like this:
<TopElement attr1="value1" attr2="value2">
This is the text I want.
<ChildElement1>This is text I don't want.</ChildElement1>
<ChildElement2>This is more text I don't want.</ChildElement2>
</TopElement>
If I go with element.InnerText
, what I get is this:
This is the text I want.This is text I don't want.This is more text I don't want.
If I want just the text within TopElement but NOT any of its children, I can do this:
Dim txt As String
For Each child As Xml.XmlNode In XmlElement.ChildNodes
If TypeOf child Is Xml.XmlText Then
txt = child.InnerText
Exit For
End If
Next
But this seems quite silly to me. Surely there is a better way?
EDIT: Sorry I didn't specify this initially: I'm looking for a solution not involving LINQ (we're in the Dark Ages over here with .NET 2.0).