XML Serialization is giving a strange error on deserialization (System.InvalidOperationException)

XML Serialization is giving a strange error on deserialization (System.InvalidOperationException)

Post by Dan Terril » Thu, 31 Oct 2002 06:14:58



I'm going through a switch from binary serialization to Xml serialization,
and things aren't going all that smoothly. I can serialize an object to a
text stream and everything is fine. However, when I go to deserialize things
everything goes kerfluey and I get a System.InvalidOperationException. The
Message isn't very helpful: "There is an error in XML document (2, 2).".

At first I thought the text was getting goofy when I loaded it, but that
turned out not to be the case. I am able to load it into an XML reader with
no problems, and it serves fine as an xml document. I can also validate the
XML text on my own. No problems there -- it just won't deserialize.

Below is the exception information if it helps, along with the code I use to
serialize and deserialize. I'll post the XML if that'd be useful, but it's a
big file.

My questions to you folks are these:

1: Any idea what's going on? Or maybe where to look?

2: How can I debug the serialization and deserialization process? The
exception info is pretty weak, and I can't think of another way to look into
the black box and see what's going on.

3: I was using binary serialization for quite awhile,and that went
swimmingly. However, I ended up changing my quote object, and all of a
sudden I couldn't use the older serialized data. Is there any way around
that, or am I stuck doing the XML serialization to avoid the problem?

Thanks in advance for your assistance.

Serialization code:

XmlSerializer b = new XmlSerializer(typeof(SerializableQuote));
MemoryStream ms = new MemoryStream();

SerializableQuote sqr = new SerializableQuote(q);

b.Serialize(ms, sqr);

byte[] bData = ms.GetBuffer();

string s = Encoding.ASCII.GetString(bData);

//do some stuff to save S to a database, etc.

Deserialization code:
string strQuote = (string)cmdRetrieveArchive.ExecuteScalar();

XmlSerializer formatter = new XmlSerializer(typeof(SerializableQuote));

StringReader stringreader = new StringReader(strQuote);

XmlTextReader r = new XmlTextReader(stringreader);

SerializableQuote q = (SerializableQuote)formatter.Deserialize(r);

the Exception:
{"There is an error in XML document (2, 2)." }
    [System.InvalidOperationException]: {System.InvalidOperationException}
    System.Object: {System.InvalidOperationException}
    _COMPlusExceptionCode: -532459699
    _className: null
    _exceptionMethod: null
    _exceptionMethodString: null
    _message: "There is an error in XML document (2, 2)."
    _innerException: {"Object reference not set to an instance of an
object." }
    _helpURL: null
    _stackTrace: {System.Array}
    _stackTraceString: null
    _remoteStackTraceString: null
    _remoteStackIndex: 0
    _HResult: -2146233079
    _source: null
    _xptrs: 0
    _xcode: -532459699
    Message: "There is an error in XML document (2, 2)."
    InnerException: {"Object reference not set to an instance of an
object." }
    TargetSite: {System.Reflection.RuntimeMethodInfo}
    StackTrace: "   at
System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader)
   at System.Xml.Serialization.XmlSerializer.Deserialize(Stream stream)
   at BenefitCentral.Entities.Quote.GetArchivedQuote(Int32 iQuoteid,
SqlConnection conn) in c:\bcdotnet\entities\quote.cs:line 205
   at BenefitCentral.Tester.lstQuotes_SelectedIndexChanged(Object sender,
EventArgs e) in c:\bcdotnet\tester.aspx.cs:line 436"
    HelpLink: ""
    Source: "System.Xml"
    HResult: -2146233079

 
 
 

XML Serialization is giving a strange error on deserialization (System.InvalidOperationException)

Post by Christoph Schittk » Thu, 31 Oct 2002 11:04:57



Quote:> I'm going through a switch from binary serialization to Xml serialization,
> and things aren't going all that smoothly.

Yes, Binary serialization and Xml Serialization are two entirely different
beasts and they shouldn't be mixed and matched. Take a look at the
SoapFormatter in System.Runtime.Serialization.Formatter.Soap if you set up
classes to serialize with the BinaryFormatter to an XML based format.

Quote:> I can serialize an object to a
> text stream and everything is fine. However, when I go to deserialize
things
> everything goes kerfluey and I get a System.InvalidOperationException. The
> Message isn't very helpful: "There is an error in XML document (2, 2).".

> At first I thought the text was getting goofy when I loaded it, but that
> turned out not to be the case. I am able to load it into an XML reader
with
> no problems, and it serves fine as an xml document. I can also validate
the
> XML text on my own. No problems there -- it just won't deserialize.

Your serialization code looks fine. Using the StreamReader to read the
serialized XML from the MemoryStream may work better, but say that you are
able to verify that the XML you get back from the Database. If you are able
to read the XML into an XmlDocument then we should look at the
SerializableQuote class if there is anything suspicious in there.

Quote:> My questions to you folks are these:

> 1: Any idea what's going on? Or maybe where to look?

Nope, nothing you posted rings a bell.

Quote:> 2: How can I debug the serialization and deserialization process? The
> exception info is pretty weak, and I can't think of another way to look
into
> the black box and see what's going on.

Agreed. Usually the InnerException gives you good information, but it
doesn't here.

Quote:> 3: I was using binary serialization for quite awhile,and that went
> swimmingly. However, I ended up changing my quote object, and all of a
> sudden I couldn't use the older serialized data. Is there any way around
> that, or am I stuck doing the XML serialization to avoid the problem?

Are you saying you serialized some objects with the BinaryFormatter, changed
the class and now you can't read it back? One way to work around this is to
register a SerializationSurrogate with the formatter for the class that
changed. Then the formatter will call into the surrogate to process the
serialized data and you can initialize the returned object yourself with the
values that you serialized. Going forward, you could implement ISerializable
to handle serialization and deserialization explicitly. It's a little more
coding, but will save you from versioning issues.

Maybe we can find the problem if you post your SerializableQuote class and
the first 5 lines of the serialized XML document.

HTH,
Christoph

 
 
 

XML Serialization is giving a strange error on deserialization (System.InvalidOperationException)

Post by David Boa » Tue, 05 Nov 2002 17:07:33


check if your class has a default constrctor (with no params).


Quote:> I'm going through a switch from binary serialization to Xml serialization,
> and things aren't going all that smoothly. I can serialize an object to a
> text stream and everything is fine. However, when I go to deserialize
things
> everything goes kerfluey and I get a System.InvalidOperationException. The
> Message isn't very helpful: "There is an error in XML document (2, 2).".

> At first I thought the text was getting goofy when I loaded it, but that
> turned out not to be the case. I am able to load it into an XML reader
with
> no problems, and it serves fine as an xml document. I can also validate
the
> XML text on my own. No problems there -- it just won't deserialize.

> Below is the exception information if it helps, along with the code I use
to
> serialize and deserialize. I'll post the XML if that'd be useful, but it's
a
> big file.

> My questions to you folks are these:

> 1: Any idea what's going on? Or maybe where to look?

> 2: How can I debug the serialization and deserialization process? The
> exception info is pretty weak, and I can't think of another way to look
into
> the black box and see what's going on.

> 3: I was using binary serialization for quite awhile,and that went
> swimmingly. However, I ended up changing my quote object, and all of a
> sudden I couldn't use the older serialized data. Is there any way around
> that, or am I stuck doing the XML serialization to avoid the problem?

> Thanks in advance for your assistance.

> Serialization code:

> XmlSerializer b = new XmlSerializer(typeof(SerializableQuote));
> MemoryStream ms = new MemoryStream();

> SerializableQuote sqr = new SerializableQuote(q);

> b.Serialize(ms, sqr);

> byte[] bData = ms.GetBuffer();

> string s = Encoding.ASCII.GetString(bData);

> //do some stuff to save S to a database, etc.

> Deserialization code:
> string strQuote = (string)cmdRetrieveArchive.ExecuteScalar();

> XmlSerializer formatter = new XmlSerializer(typeof(SerializableQuote));

> StringReader stringreader = new StringReader(strQuote);

> XmlTextReader r = new XmlTextReader(stringreader);

> SerializableQuote q = (SerializableQuote)formatter.Deserialize(r);

> the Exception:
> {"There is an error in XML document (2, 2)." }
>     [System.InvalidOperationException]: {System.InvalidOperationException}
>     System.Object: {System.InvalidOperationException}
>     _COMPlusExceptionCode: -532459699
>     _className: null
>     _exceptionMethod: null
>     _exceptionMethodString: null
>     _message: "There is an error in XML document (2, 2)."
>     _innerException: {"Object reference not set to an instance of an
> object." }
>     _helpURL: null
>     _stackTrace: {System.Array}
>     _stackTraceString: null
>     _remoteStackTraceString: null
>     _remoteStackIndex: 0
>     _HResult: -2146233079
>     _source: null
>     _xptrs: 0
>     _xcode: -532459699
>     Message: "There is an error in XML document (2, 2)."
>     InnerException: {"Object reference not set to an instance of an
> object." }
>     TargetSite: {System.Reflection.RuntimeMethodInfo}
>     StackTrace: "   at
> System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader)
>    at System.Xml.Serialization.XmlSerializer.Deserialize(Stream stream)
>    at BenefitCentral.Entities.Quote.GetArchivedQuote(Int32 iQuoteid,
> SqlConnection conn) in c:\bcdotnet\entities\quote.cs:line 205
>    at BenefitCentral.Tester.lstQuotes_SelectedIndexChanged(Object sender,
> EventArgs e) in c:\bcdotnet\tester.aspx.cs:line 436"
>     HelpLink: ""
>     Source: "System.Xml"
>     HResult: -2146233079

 
 
 

1. Web Services Problem (System.InvalidOperationException)

We have a few Web services that we have developed
using .NET and we have had no problems consuming them
using the full .NET framework.  The problem is that when
we reference the same web services from a .NET Compact
Framework project we get an exception when the Web
service is invoked.  We are using VS.NET 2003 Final
Beta.  Can anyone help us here?  We have not idea why
this is happening.  Here is the exception we get:

************** Exception Text **************
System.InvalidOperationException: Method Test.Process can
not be reflected. ---> System.IO.FileNotFoundException:
File or assembly name System.Web.Services, or one of its
dependencies, was not found.
File name: "System.Web.Services"
   at
System.Reflection.CustomAttribute.GetDataForPropertyOrFiel
d(Boolean& isProperty, Object& value, Type& type, Boolean
isLast)
   at System.Reflection.CustomAttribute.GetObject()
   at
System.Reflection.CustomAttribute.CheckConsistencyAndCreat
eArray(CustomAttribute caItem, Type caType)
   at
System.Reflection.CustomAttribute.GetCustomAttributes
(MemberInfo member, Type caType, Boolean inherit)
   at
System.Reflection.RuntimeMethodInfo.GetCustomAttributes
(Type attributeType, Boolean inherit)
   at
System.Web.Services.Protocols.LogicalMethodInfo.GetCustomA
ttributes(Type type)
   at
System.Web.Services.Protocols.SoapReflector.GetSoapMethodA
ttribute(LogicalMethodInfo methodInfo)
   at
System.Web.Services.Protocols.SoapReflector.ReflectMethod
(LogicalMethodInfo methodInfo, Boolean client,
XmlReflectionImporter xmlImporter, SoapReflectionImporter
soapImporter, String defaultNs)

2. Pop Up

3. System.Xml.Serialization.IO.XmlWriter vs. System.Xml.XmlWriter

4. CG Fonts

5. System.Xml.Serialization.IO.XmlTextReader -vs- System.Xml.XmlReader

6. CVS documentation translation

7. Error trying to generate a value for a System.Data.OleDb.OleDbConnection type property

8. REQ: Drive Diagnostic Utility

9. System.Security.SecurityException: Security error.

10. System.Xml.Serialization.Schema supports simpletype?

11. Httpwebresponse.responsestream() is giving system not supported error