XML Schema Validation + Deserialization

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Shone

    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
  • Mickey Williams

    #2
    Re: XML Schema Validation + Deserialization

    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




    "Shone" <shonend@yahoo. com> wrote in message
    news:38885f3a.0 404300656.60106 353@posting.goo gle.com...[color=blue]
    > 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[/color]


    Comment

    • Shone

      #3
      Re: XML Schema Validation + Deserialization

      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

      Comment

      Working...