471,595 Members | 2,130 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

XML Validating Reader problem


From: "R. Raghuraman" <ra********@infosys.com>
Subject: Problem with XMLValidatingReader?
Date: 01 April 2004 11:46

Hi,
I have a large xml file to process (~ 70 mb). I am trying to use
XMLValidating reader. The first step was to try out the example in
quickstart samples with minor modifications [significant portions
below]. My intention is to validate the file against a DTD before
processing and to collect all validation errors in a single pass.

Public Class ValidationReadingXMLSample

Private WithEvents myXmlValidatingReader As XmlValidatingReader
Private myXmlTextReader As XmlTextReader

Private Sub Validate()

Try
Dim valdel As ValidationEventHandler = New
ValidationEventHandler(AddressOf ValidationEvent)

While myXmlValidatingReader.Read()
End While

Console.WriteLine("Validation finished. Validation {0}",
s)
Catch e As XmlException
Console.WriteLine("XmlException: {0} ", e.ToString())
Catch e As Exception
Console.WriteLine("Exception: {0} ", e.ToString())
End Try
End Sub

Public Sub ValidationEvent(ByVal errorid As Object, ByVal args As
ValidationEventArgs) Handles
myXmlValidatingReader.ValidationEventHandler

Console.WriteLine(Strings.Chr(9) & "Validation error: " &
args.Message)

If (args.Severity = XmlSeverityType.Warning) Then
Console.WriteLine("No schema found to enforce
validation.")
ElseIf (args.Severity = XmlSeverityType.Error) Then
Console.WriteLine("validation error occurred when
validating the instance document.")
End If

If Not (args.Exception Is Nothing) Then ' XSD schema
validation error
Console.WriteLine(args.Exception.SourceUri & "," &
args.Exception.LinePosition & "," & args.Exception.LineNumber)
End If

End Sub

End Class
The sample works fine for the first 3 errors and then throws an
exception at

While myXmlValidatingReader.Read()

due to which the rest of the file is not being processed. Is this the
way it's supposed to work? This does not solve my problem of trying to
parse the whole file and getting all the errors in one go.

Furthermore, the following code works as follows

While Not myXmlValidatingReader.EOF
Try
While myXmlValidatingReader.Read()
End While
Catch e As XmlException
Console.WriteLine("XmlException: {0} ",
e.ToString())
myXmlValidatingReader.Read()
End Try
End While

But I fail to understand that if this line
While myXmlValidatingReader.Read()
throws an exception, how is it that the line in the catch block
succeeds
myXmlValidatingReader.Read()
Thanks in advance,
Raghu
Nov 12 '05 #1
2 2555
XmlValidatingReader is a forward only cursor on the underlying Xml. I would
expect the Read() in the catch block to succeed, because it would be reading
the token next to the one that caused the error in your try block.

When you inspect the exception object in the catch block what is the message
you get?

----------------
"Raghu R" <r.***********@ntlworld.com> wrote in message
news:Ad%ac.183$Nc4.129@newsfe1-win...

From: "R. Raghuraman" <ra********@infosys.com>
Subject: Problem with XMLValidatingReader?
Date: 01 April 2004 11:46

Hi,
I have a large xml file to process (~ 70 mb). I am trying to use
XMLValidating reader. The first step was to try out the example in
quickstart samples with minor modifications [significant portions
below]. My intention is to validate the file against a DTD before
processing and to collect all validation errors in a single pass.

Public Class ValidationReadingXMLSample

Private WithEvents myXmlValidatingReader As XmlValidatingReader
Private myXmlTextReader As XmlTextReader

Private Sub Validate()

Try
Dim valdel As ValidationEventHandler = New
ValidationEventHandler(AddressOf ValidationEvent)

While myXmlValidatingReader.Read()
End While

Console.WriteLine("Validation finished. Validation {0}",
s)
Catch e As XmlException
Console.WriteLine("XmlException: {0} ", e.ToString())
Catch e As Exception
Console.WriteLine("Exception: {0} ", e.ToString())
End Try
End Sub

Public Sub ValidationEvent(ByVal errorid As Object, ByVal args As
ValidationEventArgs) Handles
myXmlValidatingReader.ValidationEventHandler

Console.WriteLine(Strings.Chr(9) & "Validation error: " &
args.Message)

If (args.Severity = XmlSeverityType.Warning) Then
Console.WriteLine("No schema found to enforce
validation.")
ElseIf (args.Severity = XmlSeverityType.Error) Then
Console.WriteLine("validation error occurred when
validating the instance document.")
End If

If Not (args.Exception Is Nothing) Then ' XSD schema
validation error
Console.WriteLine(args.Exception.SourceUri & "," &
args.Exception.LinePosition & "," & args.Exception.LineNumber)
End If

End Sub

End Class
The sample works fine for the first 3 errors and then throws an
exception at

While myXmlValidatingReader.Read()

due to which the rest of the file is not being processed. Is this the
way it's supposed to work? This does not solve my problem of trying to
parse the whole file and getting all the errors in one go.

Furthermore, the following code works as follows

While Not myXmlValidatingReader.EOF
Try
While myXmlValidatingReader.Read()
End While
Catch e As XmlException
Console.WriteLine("XmlException: {0} ",
e.ToString())
myXmlValidatingReader.Read()
End Try
End While

But I fail to understand that if this line
While myXmlValidatingReader.Read()
throws an exception, how is it that the line in the catch block
succeeds
myXmlValidatingReader.Read()
Thanks in advance,
Raghu

Nov 12 '05 #2
Hi,
I think I've figured it out - the exceptions I get are of the nature
where the XML is not well formed. MSDN says that XmlTextReader checks for
well-formedness of the document - hence I think that the exception is being
raised by the embedded xmltextreader which is then propogated above. Also,
this bypasses the event handler (but then, in hind sight, it seems obvious)
Code I have works but seems chunky - with the error handling code spread
over two parts....

Thanks!
Raghu
"NaraendiraKumar R. R." <na****@nospam.com> wrote in message
news:Of**************@TK2MSFTNGP10.phx.gbl...
XmlValidatingReader is a forward only cursor on the underlying Xml. I would expect the Read() in the catch block to succeed, because it would be reading the token next to the one that caused the error in your try block.

When you inspect the exception object in the catch block what is the message you get?

----------------
"Raghu R" <r.***********@ntlworld.com> wrote in message
news:Ad%ac.183$Nc4.129@newsfe1-win...

From: "R. Raghuraman" <ra********@infosys.com>
Subject: Problem with XMLValidatingReader?
Date: 01 April 2004 11:46

Hi,
I have a large xml file to process (~ 70 mb). I am trying to use
XMLValidating reader. The first step was to try out the example in
quickstart samples with minor modifications [significant portions
below]. My intention is to validate the file against a DTD before
processing and to collect all validation errors in a single pass.

Public Class ValidationReadingXMLSample

Private WithEvents myXmlValidatingReader As XmlValidatingReader
Private myXmlTextReader As XmlTextReader

Private Sub Validate()

Try
Dim valdel As ValidationEventHandler = New
ValidationEventHandler(AddressOf ValidationEvent)

While myXmlValidatingReader.Read()
End While

Console.WriteLine("Validation finished. Validation {0}",
s)
Catch e As XmlException
Console.WriteLine("XmlException: {0} ", e.ToString())
Catch e As Exception
Console.WriteLine("Exception: {0} ", e.ToString())
End Try
End Sub

Public Sub ValidationEvent(ByVal errorid As Object, ByVal args As
ValidationEventArgs) Handles
myXmlValidatingReader.ValidationEventHandler

Console.WriteLine(Strings.Chr(9) & "Validation error: " &
args.Message)

If (args.Severity = XmlSeverityType.Warning) Then
Console.WriteLine("No schema found to enforce
validation.")
ElseIf (args.Severity = XmlSeverityType.Error) Then
Console.WriteLine("validation error occurred when
validating the instance document.")
End If

If Not (args.Exception Is Nothing) Then ' XSD schema
validation error
Console.WriteLine(args.Exception.SourceUri & "," &
args.Exception.LinePosition & "," & args.Exception.LineNumber)
End If

End Sub

End Class
The sample works fine for the first 3 errors and then throws an
exception at

While myXmlValidatingReader.Read()

due to which the rest of the file is not being processed. Is this the
way it's supposed to work? This does not solve my problem of trying to
parse the whole file and getting all the errors in one go.

Furthermore, the following code works as follows

While Not myXmlValidatingReader.EOF
Try
While myXmlValidatingReader.Read()
End While
Catch e As XmlException
Console.WriteLine("XmlException: {0} ",
e.ToString())
myXmlValidatingReader.Read()
End Try
End While

But I fail to understand that if this line
While myXmlValidatingReader.Read()
throws an exception, how is it that the line in the catch block
succeeds
myXmlValidatingReader.Read()
Thanks in advance,
Raghu


Nov 12 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Robert Reineri | last post: by
2 posts views Thread by Joris Janssens | last post: by
1 post views Thread by Craig Beuker | last post: by
reply views Thread by Jan Eliasen | last post: by
21 posts views Thread by Jack White | last post: by
4 posts views Thread by agda.karlberg | last post: by
reply views Thread by XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by

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.