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
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
Comment