XmlTextReader not getting all Elements

G

Guest

Hi All,
I have something going wrong with the XmlTextReader. I have a function that
reads the following XML example. For some reason the code is only getting two
of the 3 Values elements. Here is some code I'm using:
Thanks for any suggestions.

<?xml version="1.0" encoding="windows-1252" ?>
<MMTP>
<LookUp>
<ControlType>C1ComboBoxList</ControlType>
<DBField>LastMedDate</DBField>
<BookMark></BookMark>
<ColumnIndex></ColumnIndex>
<ControlName>cmbGoal5</ControlName>
<Values VALUE="0" BookMark="Goal5Progress"/>
<Values VALUE="1" BookMark="Goal5NoProgress"/>
<Values VALUE="2" BookMark="Goal5NA"/>
</LookUp>
</MMTP>

Private Shared Function FormatXml(ByVal reader As XmlTextReader, ByVal
filename As String) As clsReportElements
Dim ReportElement As clsReportElement
Dim al As new clsReportElements 'ArrayList(117)
Dim lValue as string
Dim lBookmark as String
Try
ReportElement = New clsReportElement
While reader.Read()
Select Case (reader.NodeType)
Case XmlNodeType.Element
'''***Other Elements removed for easy reading*****
If (reader.Name = "Values") Then
dim isValueAdded as Boolean = False
if reader.HasAttributes then
Do while reader.MoveToNextAttribute ()
if len(Reader.Value.Trim) > 0 then
if Reader.Name = "VALUE" then
lValue = Reader.Value
End If
end if
if Reader.Name = "BookMark" then
if len(Reader.Value.Trim) > 0 then
lBookmark = Reader.Value.ToString
isValueAdded = True
end if
End If
Loop
End If
if isValueAdded = True then
ReportElement.AddValue (lValue, lBookmark)
end if
End If
case XmlNodeType.EndElement
If (reader.Name = "LookUp") then
al.Add(ReportElement)
ReportElement = New clsReportElement
End If
case xmlnodetype.Attribute
If (reader.Name = "Values") Then
ReportElement.AddValue (0, reader.ReadElementString())
End If
End Select
End While
Return al
Catch ex As Exception
Console.WriteLine("Operation Failed.")
Console.WriteLine("Exception: {0}", ex.ToString())
Finally
'Finished with XmlTextReader
If Not reader Is Nothing Then
reader.Close()
End If
End Try

End Function

Michael Lee
 
M

Marcie Jones

Hi Michael,
I just tried your code and all three VALUE items are being picked up
for me. Perhaps your problem isn't where you think, can you
elaborate?

Marcie
 
M

Marcie Jones

So my thought was to be able to have more than one Values element for
the comboboxs and checkboxs, but for textboxs I don't need the values element
so I leave one empty Values element for that lookup. In the main file do you
see any problems with the parsers because of that?

No, I think that should be fine.

I tweaked a few things to get the example to work, I'll paste in
exactly what I used in case that's a help. Pardon the line breaks:

Sub Michael()
Dim fn As String = "C:\Documents and
Settings\Administrator\My Documents\Visual Studio
Projects\XmlHarry\MMTP.xml"
Dim xr As XmlTextReader = New
XmlTextReader(fn)
Dim al As ArrayList = FormatXml(xr, fn)
Dim ht As System.Collections.Hashtable =
CType(al(0), System.Collections.Hashtable)
MessageBox.Show(ht.Count)

End Sub
Private Shared Function FormatXml(ByVal reader As
XmlTextReader, ByVal filename As String) As
System.Collections.ArrayList
Dim ReportElement As
System.Collections.Hashtable
Dim al As New ArrayList(117)
Dim lValue As String
Dim lBookmark As String
Try
ReportElement = New
System.Collections.Hashtable
While reader.Read()
Select Case (reader.NodeType)
Case XmlNodeType.Element
'''***Other Elements removed
for easy reading*****
If (reader.Name = "Values")
Then
Dim isValueAdded As
Boolean = False
If reader.HasAttributes
Then
Do While
reader.MoveToNextAttribute()
If
Len(reader.Value.Trim) > 0 Then
If reader.Name
= "VALUE" Then
lValue =
reader.Value
End If
End If
If reader.Name =
"BookMark" Then
If
Len(reader.Value.Trim) > 0 Then
lBookmark
= reader.Value.ToString

isValueAdded = True
End If
End If
Loop
End If
If isValueAdded = True
Then

ReportElement.Add(lValue, lBookmark)
End If
End If
Case XmlNodeType.EndElement
If (reader.Name = "LookUp")
Then
al.Add(ReportElement)
ReportElement = New
System.Collections.Hashtable
End If
Case XmlNodeType.Attribute
If (reader.Name = "Values")
Then
ReportElement.Add(0,
reader.ReadElementString())
End If
End Select
End While
Return al
Catch ex As Exception
MessageBox.Show("Operation Failed.")
MessageBox.Show("Exception: {0}",
ex.ToString())
Finally
'Finished with XmlTextReader
If Not reader Is Nothing Then
reader.Close()
End If
End Try

End Function
 

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