De/Serialization problem

T

Tamir Khason

I have an object (eg MyObject with namespace eg MyNamespace)

[XmlRoot("MyObject", Namespace="MyNamespace", IsNullable=false)]

public class MyObject


While I serialize it the XML created is
<MyObject xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="MyNamespace">
<MyObjectCollection Name="MyCollection">
<ObjectState>Null</ObjectState>
</MyObjectCollection>
</MyObject>

and this works fine.
While I'm trying to Deselialize it I recieve an error:
System.InvalidOperationException: There is an error in XML document (1,
2). ---> System.InvalidOperationException: <MyObject xmlns='MyNamespace'>
was not expected.

WHY? The same object, the same namespace, everything is the same.

Trying to remove namespace. The same error:

System.InvalidOperationException: There is an error in XML document (1,
2). ---> System.InvalidOperationException: <MyObject xmlns=''> was not
expected.

Strange...
Please advice...
 
K

Kevin Yu [MSFT]

Hi Tamir,

First of all, I would like to confirm my understanding of your issue. From
your description, I understand that when you're trying to deserialize from
an Xml file to an object, an error which says there is an error in the Xml
document. If there is any misunderstanding, please feel free to let me know.

I have written some code and was able to reproduce this error. Based on my
research, the XmlSerializer.Serialize method will not close the text writer
object automatically after serialization. That might be the cause of this
exception. So the solution is we close the text writer manually after
serialization. Here is a code snippet. HTH.

XmlSerializer s = new XmlSerializer(typeof(MyObject),"MyNamespace");
XmlTextWriter w = new XmlTextWriter(@"c:\aaaaser.xml",
System.Text.Encoding.UTF8);
MyObject o = new MyObject();
o.a = 10;
s.Serialize(w, o);
Console.WriteLine("Serialization OK!");

w.Close(); //Close the Text Writer manually

XmlTextReader r = new XmlTextReader(@"c:\aaaaser.xml");
MyObject o1 = (MyObject)s.Deserialize(r);
Console.WriteLine(o1.a.ToString());
Console.ReadLine();

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
T

Tamir Khason

I'm closing the writer object, that's not the point. Following serialization
code:
SomeObject myobject = WHATEVER();

XmlDocument doc = new XmlDocument();

XmlSerializer s = new XmlSerializer( typeof( SomeObject) );

System.IO.Stream stream = new System.IO.MemoryStream();

XmlWriter w = new XmlTextWriter(stream,System.Text.Encoding.UTF8 );

s.Serialize( w, myobject);

stream.Flush();

stream.Position = 0;

doc.Load(stream);

w.Close();


stream.Close();

return doc;

Please advice
 
K

Kevin Yu [MSFT]

Hi Tamir,

Based on your code, the object can be serialized successfully to the memory
stream. However, we don't need to use an XmlTextWriter. The Serialize
method can use a stream as target directly. When deserialize from the
stream. We also have to set the position to 0. That might be the cause of
the error. Here I have written a code snippet. HTH.

//Serialize
MyObject myobject = new MyObject();
myobject.a = 10;
XmlDocument doc = new XmlDocument();
XmlSerializer s = new XmlSerializer(typeof(MyObject));
System.IO.Stream stream = new System.IO.MemoryStream();
s.Serialize(stream, myobject);
stream.Flush();
stream.Position = 0;
doc.Load(stream);

//Deserialize
stream.Position=0;
MyObject o1 = (MyObject)s.Deserialize(stream);

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
T

Tamir Khason

Still problems. I'm continue to recieve
System.InvalidOperationException: There is an error in XML document (1,
2). ---> System.InvalidOperationException: <MyObject xmlns='SomeObject'> was
not expected.



Advice?
 
K

Kevin Yu [MSFT]

Hi Tamir,

I cannot reproduce it on my machine. Everything works fine after I add
stream.Position = 0; before deserialization. If that still doesn't work,
there might be something wrong the the object you have serialized. Could
you create a new console application and try to serialize and deserialize a
single object (such as an XmlDocument) using the code I have provided?

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top