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 [quoted text, click to view] "Shone" <shonend@yahoo.com> wrote in message
news:38885f3a.0404300656.60106353@posting.google.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(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