473,383 Members | 1,829 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

XmlTextReader not getting all Elements

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

Nov 21 '05 #1
2 1362
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

On Tue, 12 Apr 2005 12:32:34 -0700, "Michael"
<Mi*****@discussions.microsoft.com> wrote:
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


Nov 21 '05 #2
On Wed, 13 Apr 2005 06:06:03 -0700, "Michael"
<Mi*****@discussions.microsoft.com> wrote:
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

Nov 21 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Meir S. | last post by:
I think the following is a bug in XmlTextReader: I need to process large XMLs, that are typically constructed of many small elements nested in the root element. Each inner element represents a...
0
by: Blink | last post by:
I'm using the XmlTextReader class to parse an XML feed. I need to search for elements that may or may not be there; for example, if there was an error with my request the XML will contain an...
2
by: Yuriy | last post by:
Hi, any ideas how to read XML fragment from TextReader? XmlTextReader constructor accepts only Stream or string as source Do I miss something? thanks Yuriy
2
by: ecomputerdelicacy | last post by:
Compact Framework SP3 (and I think also SP2 and SP1) XmlTextReader does not handle !DOCTYPE and (maybe?) rdf elements. How can I get the XmlTextReader to read these without issuing a...
11
by: kenneth | last post by:
I just ran across this. #1 <DBColumn> 1 </DBColumn> #2 <DBColumn> </DBColumn> The data for #1 will be parsed and returned as " 1 ". I get a sequence of...
4
by: Paul Bromley | last post by:
I thought that XMLTextReader would be simple to use, but I have run into problems with it! I seem to have great difficulty extrcting the text of specific elements from a very simple XML file. I...
4
by: CodeRazor | last post by:
I am trying to use an XmlTextReader to retrieve data. I need to use an XmlTextReader because it is faster than using an XmlDocument. I have found an inelegant way of retrieving each item's title...
2
by: JC | last post by:
Hi, I am starting out an xml file, example's below. It will be like that but larger. I am having problems actually getting at the inner nodes. Example the items I named InputOne, State,...
1
by: lejason | last post by:
Hi, I have an XML file that is a list of product models and info. The list will eventually contain about 100 products, each product having multiple elements for things like names,...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.