I would like to perform a 2-pass XML reading from a stream. Once using
the Validating reader, just to confirm the validity against the
schema, and next time to do a reading to extract the data. Actually,
second time I do a deserialization, the data from XML is fed directly
to an object.
The problem I am experiencing is the error at the second reading
attempt, and error description implies that reader is winded to the
end of the XML and tries to read from there.
Let me introduce you to the code first (VB.NET):
(>>> the source XML is in InStream <<<)
' xmlReader - reads from the stream
xmlRd = New XmlTextReader(InStream)
' xml validator - references the xml reader, uses schema
xmlVal = New XmlValidatingReader(xmlRd)
xmlVal.ValidationType = ValidationType.Schema
xmlSchemas = New XmlSchemaCollection()
' add (only) one schema to the collection
xmlSchemas.Add(Nothing, "Schema1.xsd")
xmlVal.Schemas.Add(xmlSchemas)
' now the validation read – XmlSchemaException exceptions
' are cought in the code not shown here
While xmlVal.Read()
End While
' XML passes the schema validation, now I try to
' "rewind" the underlying XML reader and stream
xmlRd.ResetState()
InStream.Position = 0
' and now the reading that is supposed to extract the data,
' actually I want to deserialize the XML into the
' appropriate object
Dim obj As TaxOrder
Dim serializer As New XmlSerializer(GetType(TaxOrder))
obj = CType(serializer.Deserialize(xmlRd), TaxOrder)
The last statement throws an InvalidOperation exception "There is an
error in XML document (0,0)", which tells me that the deserializer is
trying to read from the end of the XML. It seems that "rewinding" was
unsuccesful and XML reader and/or underlying stream stil have the
EOF=true.
If I skip the validation reading (comment out the While-EndWhile
pair), everything does well, and the XML is correctly deserialized.
But that's not an option, I need that schema validation.
What else did I try: validation read is skipped, but in the last
statement I use XmlVal:
obj = CType(serializer.Deserialize(xmlVal), TaxOrder)
This would perform the validation during the deserialization, but the
exception thrown in case of invalid XML is of type
InvalidOperationException, not XmlSchemaException. The consequence,
you can guess, is the error message too general ("There is an error in
the XML document.") and non-descriptive enough, to point to exact
place in XML that is incorrect. That's why I need the validation
reading (or any other solution ?).
Any thoughts will be highly appreciated, thanks so much!
Shone 2 9335
What's the point of doing it twice? Why not use the validating reader to
deserialize the stream into your instance? If the validating reader
encounters a document that isn't schema-valid, you can deal with it as
you're deserializing.
--
Mickey Williams
Author, "Microsoft Visual C# .NET Core Reference", MS Press www.servergeek.com
"Shone" <sh*****@yahoo.com> wrote in message
news:38**************************@posting.google.c om... I would like to perform a 2-pass XML reading from a stream. Once using the Validating reader, just to confirm the validity against the schema, and next time to do a reading to extract the data. Actually, second time I do a deserialization, the data from XML is fed directly to an object.
The problem I am experiencing is the error at the second reading attempt, and error description implies that reader is winded to the end of the XML and tries to read from there.
Let me introduce you to the code first (VB.NET):
(>>> the source XML is in InStream <<<)
' xmlReader - reads from the stream xmlRd = New XmlTextReader(InStream) ' xml validator - references the xml reader, uses schema xmlVal = New XmlValidatingReader(xmlRd) xmlVal.ValidationType = ValidationType.Schema xmlSchemas = New XmlSchemaCollection() ' add (only) one schema to the collection xmlSchemas.Add(Nothing, "Schema1.xsd") xmlVal.Schemas.Add(xmlSchemas) ' now the validation read - XmlSchemaException exceptions ' are cought in the code not shown here While xmlVal.Read() End While ' XML passes the schema validation, now I try to ' "rewind" the underlying XML reader and stream xmlRd.ResetState() InStream.Position = 0 ' and now the reading that is supposed to extract the data, ' actually I want to deserialize the XML into the ' appropriate object Dim obj As TaxOrder Dim serializer As New XmlSerializer(GetType(TaxOrder)) obj = CType(serializer.Deserialize(xmlRd), TaxOrder)
The last statement throws an InvalidOperation exception "There is an error in XML document (0,0)", which tells me that the deserializer is trying to read from the end of the XML. It seems that "rewinding" was unsuccesful and XML reader and/or underlying stream stil have the EOF=true. If I skip the validation reading (comment out the While-EndWhile pair), everything does well, and the XML is correctly deserialized. But that's not an option, I need that schema validation. What else did I try: validation read is skipped, but in the last statement I use XmlVal:
obj = CType(serializer.Deserialize(xmlVal), TaxOrder)
This would perform the validation during the deserialization, but the exception thrown in case of invalid XML is of type InvalidOperationException, not XmlSchemaException. The consequence, you can guess, is the error message too general ("There is an error in the XML document.") and non-descriptive enough, to point to exact place in XML that is incorrect. That's why I need the validation reading (or any other solution ?).
Any thoughts will be highly appreciated, thanks so much!
Shone
I have found the solution:
As I already assumed, schema-validation can be performed during the
deserialization, since this is also some sort of reading. The
exception cought (in case of schema-invalid xml) is of type
InvalidOperationException, but it's INNER exception is of
XmlSchemaException, which is exactly what I needed, with fully
descriptive schema-error.
So the code will look like this:
Try
....
obj = CType(serializer.Deserialize(xmlVal), TaxOrder)
....
Catch e as Exception
...(do something with)... e.InnerException.Message
End Try
All this makes perfect sense, because the operation that threw the
exception is "Deserialize", and the kind of exception that one can
throw is InvalidOperationException. But since the real cause of the
error is underlying xml reading, and some "invisible" operations that
are performed underneath, they can too throw an exception, and that is
exactly what happened. Xml schema validating reading threw an
XmlSchemaException, which is the inner exception for the deserializing
one.
Thanks for reading, hope this will be helpfull to someone else too.
Shone This discussion thread is closed Replies have been disabled for this discussion. Similar topics
1 post
views
Thread by Mike |
last post: by
|
4 posts
views
Thread by joes |
last post: by
|
2 posts
views
Thread by wumingshi |
last post: by
|
1 post
views
Thread by Dan Bass |
last post: by
|
2 posts
views
Thread by Ali |
last post: by
|
1 post
views
Thread by billa1972 |
last post: by
|
reply
views
Thread by Mark Parter |
last post: by
|
2 posts
views
Thread by Mark |
last post: by
|
9 posts
views
Thread by mstilli |
last post: by
| | | | | | | | | | |