Read XML Text Node Name

G

Guest

All

I am using the code below in order to read an XML file in the format:

<root>
<selection-attr attr="p70">
<value code="Mr">Mr</value>
<value code="Mrs">Mrs</value>
<value code="Ms">Ms</value>
<value code="Miss">Miss</value>
<caption>Title:</caption>
</selection-attr>
</root>

What I am trying to do is identify the element name of the text node ie.
<caption>. On an element node I can use reader.Name but I have been unable
to identify how to read the value for a text node. I would appreciate any
assistance.

Thanks

' ---------
' Code Snippet
'
Dim sr As New System.IO.StreamReader("c:\xml.xml")
Dim sw As New System.IO.StreamWriter("c:\output.txt")
Dim attr As String
Dim doc As New Xml.XmlDocument
doc.Load(sr)
Dim reader As New Xml.XmlNodeReader(doc)

While reader.Read()
Select Case reader.NodeType
Case Xml.XmlNodeType.Element
If reader.Name = "text-attr" Or reader.Name = "selection-attr" Then
attr = reader.GetAttribute("attr")
End If
Case Xml.XmlNodeType.Text
sw.WriteLine(attr & ", " & reader.Name & ", " & reader.Value)
End Select
End While

sw.Close()
 
G

Guest

Use the amlelement object. Once you have the node you want, it is very easy
to change or read the node value, or attribute. Use xpath to find it

Dim xd As New Xml.XmlDocument

'load the xml file
xd.Load("myxml.xml")

'get the value
Dim Node As Xml.XmlElement =
CType(xd.DocumentElement.SelectSingleNode("/selection/caption"),
Xml.XmlElement)

msgbox node.value
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top