Parsing XML Data Part II

  • Thread starter Thread starter Kent Ogletree
  • Start date Start date
K

Kent Ogletree

I seem to still be stuck a bit on getting started with XMLTextReader which
seem to be exactly what I need. I retrieve a fragment of data from a
database field, an example of which is (I have no control over how it is
formatted or structured):

<qMultipleChoice>
<qText>What workstation are you working at?</qText>
<qChoice>1</qChoice>
<qChoice>2</qChoice>
<qChoice>3</qChoice>
<qChoice>4</qChoice>
<qChoice>5</qChoice>
<qChoice>7</qChoice>
<qChoice>8</qChoice>
<qChoice>9</qChoice>
<qChoice>Other</qChoice>
</qMultipleChoice>

I can loop through it using the XMLTextReader and see the names of the
elements with reader.Name, getting

qMultipleChoice
qText
qChoice
qChoice......etc.

However I can not get the values. reader.value seems to return nothing. I
need to get the name of each element and the values and take certain
formatting and structure actions based on the names. I tried
reader.ReadElementString, but then I can not apparently see what the name
is. Can anyone loan me a clue-by-4 on how to get the value and names? I know
I am missing something.

Kent
 
Kent Ogletree said:
I seem to still be stuck a bit on getting started with XMLTextReader which
seem to be exactly what I need. I retrieve a fragment of data from a
database field, an example of which is (I have no control over how it is
formatted or structured):

<qMultipleChoice>
<qText>What workstation are you working at?</qText>
<qChoice>1</qChoice>
<qChoice>2</qChoice>
<qChoice>3</qChoice>
<qChoice>4</qChoice>
<qChoice>5</qChoice>
<qChoice>7</qChoice>
<qChoice>8</qChoice>
<qChoice>9</qChoice>
<qChoice>Other</qChoice>
</qMultipleChoice>

I can loop through it using the XMLTextReader and see the names of the
elements with reader.Name, getting

qMultipleChoice
qText
qChoice
qChoice......etc.

Try this:


Sub main()
Dim xr As XmlTextReader = New XmlTextReader("choice.xml")
xr.WhitespaceHandling = WhitespaceHandling.None
xr.Read()
Console.WriteLine(xr.Name)
xr.ReadStartElement()
While xr.IsStartElement
Console.WriteLine(xr.Name & " - " & xr.ReadElementString)
End While
xr.ReadEndElement()
xr.Close()
end sub

David
 
Back
Top