473,387 Members | 1,290 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,387 software developers and data experts.

XML Schema Validation + Deserialization

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
Nov 12 '05 #1
2 9430
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

Nov 12 '05 #2
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
Nov 12 '05 #3

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

Similar topics

1
by: Mike | last post by:
Note: My XML experience to date has (unfortunately) been limited to reading and thinking, rather than implementation. Anyway, I am in the process of trying to figure out the most efficient way...
4
by: joes | last post by:
Hello there I tried for several days to get a simple validation with xml schema & xerces working. Goal for me is tuse JAXP and not specific Xerces classes. I don't get the point what I am doing...
2
by: wumingshi | last post by:
Hi, When validating an XML instance, sometimes the schema is not enough to expression the validation rules. Additional validation rules may be expressed in an application-specific way. For...
1
by: Dan Bass | last post by:
There's an XML message I have, that has no namespace information. Then there is a XSD schema that is must validate against, but this has a targetNamespace and xmlns of...
2
by: Ali | last post by:
I am having problem compiling schema contained in WSDL file when analyzing schema types contained in it (for example http://www.ebout.net/net/GoogleSearch.wsdl). Following code demonstrates my...
1
by: billa1972 | last post by:
Hi, I am trying to hook into Yellow Freight's rating webservice. Below is the wsdl. When i try and create a proxy file with wsdl.exe i get the following errors, see below. Also, when i...
0
by: Mark Parter | last post by:
I'm trying convert an XML Schema (http://www.elframework.org/projects/xcri/efc_r1.0.xsd/view) to a VB.Net class using the XSD tool provided with the .NET 2 SDK. However, it's failing to generate...
2
by: Mark | last post by:
Hi... I've been trying the .Validate() method on the XmlDocument to validate some xml against a schema, but one thing I noted was that unless the document explicitly declares the schema as a...
9
by: mstilli | last post by:
Hi, I am trying to use schema for server side validation using xerces to catch the validation errors. validating this XML: <Content4> <textarea13></textarea13>...
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: 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...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.