|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
XmlTextReader not getting all ElementsI 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 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" <Mich***@discussions.microsoft.com> wrote: Show quoteHide quote >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 Hi Marcie,
Thank you so much for your reply. I'm not sure what is happening if it works for you. Using the sample xml file from the last message. I just did a test on my system again and have the following results. The reader gets first 5 fields of the Lookup section. When it gets to the Values, it skips the first one for some reason, and gets the following 2. For example: <Values VALUE="0" BookMark="Goal5Progress"/> Skips <Values VALUE="1" BookMark="Goal5NoProgress"/> Gets <Values VALUE="2" BookMark="Goal5NA"/> Gets I have a little more code in the function handling this, but its just for other elements that should not have anything to do with the problem. I have a questions on the XML file itself. This file is a config file and because of the nature of each document we have to process, the lookup section has to be slightly different on some of the sections. I've used the values element to provide a way to know the Items of say a Combobox or a checkbox, ect.... 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? This should not affect the test I'm running right now, because I only have one lookup section in the file at this time, and I still have the problem with the missing first Values element. Thanks again for the reply. If you have any suggestions I'm open. Maybe I need a DTD, Schema or something else. Thanks again. Michael Lee Show quoteHide quote "Marcie Jones" wrote: > 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" > <Mich***@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 > > On Wed, 13 Apr 2005 06:06:03 -0700, "Michael"
<Mich***@discussions.microsoft.com> wrote: > So my thought was to be able to have more than one Values element for No, I think that should be fine.>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? 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 Hi Marcie,
I made the changes you had, it worked, then put the code back the way it way. It works. I do hav it working as of know. Thanks so much for the reply. Michael Lee Show quoteHide quote "Marcie Jones" wrote: > On Wed, 13 Apr 2005 06:06:03 -0700, "Michael" > <Mich***@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 > > > >
Translate C# to VB.net?
Splash window VB.NET Equiv of... AxWebBrowser1 and code behind redirecting Assigning a Null to Datetime Variable or Object MDIParent property Timer Event Not Firing in SystemFileWatcher... [ANN] April 12, 2005, "Visual Basic 2005 Language Enhancements" chat System Dsn creation from vb.net ? ComboBox data |
|||||||||||||||||||||||