473,903 Members | 4,560 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(I nStream)
' xml validator - references the xml reader, uses schema
xmlVal = New XmlValidatingRe ader(xmlRd)
xmlVal.Validati onType = ValidationType. Schema
xmlSchemas = New XmlSchemaCollec tion()
' add (only) one schema to the collection
xmlSchemas.Add( Nothing, "Schema1.xs d")
xmlVal.Schemas. Add(xmlSchemas)
' now the validation read – XmlSchemaExcept ion 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.ResetStat e()
InStream.Positi on = 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(G etType(TaxOrder ))
obj = CType(serialize r.Deserialize(x mlRd), TaxOrder)

The last statement throws an InvalidOperatio n 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(serialize r.Deserialize(x mlVal), TaxOrder)

This would perform the validation during the deserialization , but the
exception thrown in case of invalid XML is of type
InvalidOperatio nException, not XmlSchemaExcept ion. 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 9456
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.goo gle.com...
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(I nStream)
' xml validator - references the xml reader, uses schema
xmlVal = New XmlValidatingRe ader(xmlRd)
xmlVal.Validati onType = ValidationType. Schema
xmlSchemas = New XmlSchemaCollec tion()
' add (only) one schema to the collection
xmlSchemas.Add( Nothing, "Schema1.xs d")
xmlVal.Schemas. Add(xmlSchemas)
' now the validation read - XmlSchemaExcept ion 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.ResetStat e()
InStream.Positi on = 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(G etType(TaxOrder ))
obj = CType(serialize r.Deserialize(x mlRd), TaxOrder)

The last statement throws an InvalidOperatio n 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(serialize r.Deserialize(x mlVal), TaxOrder)

This would perform the validation during the deserialization , but the
exception thrown in case of invalid XML is of type
InvalidOperatio nException, not XmlSchemaExcept ion. 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
InvalidOperatio nException, but it's INNER exception is of
XmlSchemaExcept ion, which is exactly what I needed, with fully
descriptive schema-error.

So the code will look like this:

Try
....
obj = CType(serialize r.Deserialize(x mlVal), TaxOrder)
....

Catch e as Exception
...(do something with)... e.InnerExceptio n.Message
End Try

All this makes perfect sense, because the operation that threw the
exception is "Deserializ e", and the kind of exception that one can
throw is InvalidOperatio nException. 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
XmlSchemaExcept ion, 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
2858
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 to validate and transform some very large (potentially over 100MB) XML documents. This is related to another question I will post next, but for now, I want to focus on one particular topic. The data in question has particular business rules...
4
5615
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 wrong. Could somebody help me? I didn't find any full example working on the net. Thank you for any hints! If I run the examples below, the parsers parses the file well, no vlaidation is occuring although the schema and xml file does not
2
2158
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 example, using XPath or Java method. So is there a concept and/or standard for post-schema validation or application validation? Is there any hook in XML standards that allow me to hook up with my customized validation? Additionally and more...
1
2328
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 "http://www.wbf.org/xml/b2mml-v02". How do I get this XML to validate against the Schema in C#? If I use XmlSpy (2005 home edition) to perform the validation, it first inserts namespace and schema information into the XML before validating. Validation then seems to work if...
2
7312
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 problem: using System.Diagnostics; using System.IO; using System.Xml; using System.Xml.Schema;
1
6418
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 reference this wsdl in .NET it seems to do it fine, yet there are no objects to reference except RateQuoteBeanService. In the WSDL it looks like there should be getRateQuote, and QUOTEREQUEST, etc.
0
2698
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 the class and is outputting the following error(s); Schema validation warning: Undefined complexType 'http://purl.org/dc/elements/1.1/:SimpleLiteral' is used as a base for complex type extension. Line 433, position 5. Schema validation warning:...
2
3050
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 default namespace, Validate() just lets it slide through as a success. Is there a way to Validate() either a) mandating against a particular schema, or at the very least b) supplying a default namespace to be used even when not explicitly declared?
9
3034
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> <binaryobject3></binaryobject3>
0
10003
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9851
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10882
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10504
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9692
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8055
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5897
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
4312
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3327
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.