By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,678 Members | 1,285 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,678 IT Pros & Developers. It's quick & easy.

De/Serialization problem

P: n/a
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...
--
Tamir Khason
You want dot.NET? Just ask:
"Please, www.dotnet.us "

Nov 16 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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."

Nov 16 '05 #2

P: n/a
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

--
Tamir Khason
You want dot.NET? Just ask:
"Please, www.dotnet.us "
"Kevin Yu [MSFT]" <v-****@online.microsoft.com> wrote in message
news:fM**************@cpmsftngxa06.phx.gbl...
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."

Nov 16 '05 #3

P: n/a
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."

Nov 16 '05 #4

P: n/a
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?
--
Tamir Khason
You want dot.NET? Just ask:
"Please, www.dotnet.us "
"Kevin Yu [MSFT]" <v-****@online.microsoft.com> wrote in message
news:6O**************@cpmsftngxa06.phx.gbl...
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."

Nov 16 '05 #5

P: n/a
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."

Nov 16 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.