By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,984 Members | 1,454 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,984 IT Pros & Developers. It's quick & easy.

Read XML Text Node Name

P: n/a
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()
Nov 21 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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

"David" wrote:
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()

Nov 21 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.