473,397 Members | 2,084 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

De/Serialization problem

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
5 3229
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: David K | last post by:
we are having a problem when trying to serilalize and deserialize DataTable When replacing the container to be ListArray or HashTable everything works fine The code is very straight forward (...
37
by: Ben | last post by:
Hi, there. Recently I was working on a problem where we want to save generic closures in a data structure (a vector). The closure should work for any data type and any method with pre-defined...
0
by: ktn | last post by:
Hi all, I'm a .NET beginner and I've got a problem on a program where I try to do an XML serialization. I get the following error : "An unmanaged exception of type...
1
by: andrewcw | last post by:
There is an error in XML document (1, 2). I used XML spy to create the XML and XSD. When I asked to have the XML validated it said it was OK. I used the .net SDK to generate the class. I have...
2
by: ofer | last post by:
Hi, I am working with the beta version of the new .net framework (Whidbey) and I encountered a problem with serialization that did'nt exist in the .net 2003 the situation is like this : I have...
0
by: umhlali | last post by:
I get the following exception when my VB.NET app calls a Java web service that returns an array of objects. The same call works for a single object though. So looks like there is no problem...
5
by: Harold Howe | last post by:
I am having a problem deserializing objects from a library when the following conditions exist: 1- The library is strongly named 2- The serialized file was created with version 1.0 of the...
2
by: Norman Chong | last post by:
Hiddeldi ho, I want to save an object so that I can use its content after I restart my program. I tried to solve this with serialization because someone told me that this is the correct way for...
4
by: mijalko | last post by:
Hi, I have inherited my class from System.Drawing.Printing.PrintDocument and I wish to serialize this object using XmlSerializer. And I get exception "There was an error reflecting type ...". If I...
2
by: mkvenkit.vc | last post by:
Hello, I hope this is the right place to post a question on Boost. If not, please let me know where I can post this message and I will do so. I am having a strange problem with std::string as...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.