473,324 Members | 1,678 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,324 software developers and data experts.

Serialize a class having property that return object as return type

Hi
I am not sure if we can do it and if can, then how. I have a class
with a property that return object as its return type.
when i am trying to Serialize the class it gives me error
************************************************** **************
An unhandled exception of type 'System.InvalidOperationException'
occurred in system.xml.dll
Additional information: There was an error generating the XML document.

************************************************** ***************

below listed a part of class code.

using System;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
using System.Text;

[Serializable()]
public class MyClass
{
private string _firstName;
private object _lastName;

public string FirstName
{
get{return _firstName;}
set{_firstName = value;}
}

public object LastName
{
get{return _lastName;}
set{_lastName = value;}
}
}

Nov 17 '05 #1
4 5436
A class must have a default constructor to be serialized by
XmlSerializer. That is probably the reason why you are getting the
InvalidOperationException

Regards,
Sarin

Nov 17 '05 #2
Why are you storing the last name as an object? It should be a string,
I would think.

Xml Serialization works by taking the public properties of an object
(and objects it exposes) and serializing those. If you expose the last name
as an object, then there are no public properties that it will be able to
serialize.

You will need to strongly type this in order to serialize anything.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

<su***********@gmail.com> wrote in message
news:11*********************@g14g2000cwa.googlegro ups.com...
Hi
I am not sure if we can do it and if can, then how. I have a class
with a property that return object as its return type.
when i am trying to Serialize the class it gives me error
************************************************** **************
An unhandled exception of type 'System.InvalidOperationException'
occurred in system.xml.dll
Additional information: There was an error generating the XML document.

************************************************** ***************

below listed a part of class code.

using System;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
using System.Text;

[Serializable()]
public class MyClass
{
private string _firstName;
private object _lastName;

public string FirstName
{
get{return _firstName;}
set{_firstName = value;}
}

public object LastName
{
get{return _lastName;}
set{_lastName = value;}
}
}

Nov 17 '05 #3
"Spidey" <sa*************@gmail.com> wrote in message
news:11*********************@g43g2000cwa.googlegro ups.com...
A class must have a default constructor to be serialized by
XmlSerializer. That is probably the reason why you are getting the
InvalidOperationException

Regards,
Sarin


It does have a default constructor - the compiler generates one for you if
you don;t supply your own constructor.

Can I take it the original is an example rather than the real deal - I can't
imaging why you would want the last name to be an object.

However, I can successfully serialize an object that looks like that on
version 2.0. I don;lt have a 1.1 environment to hand to test that. On 2.0
the result looks like this

<?xml version="1.0" encoding="IBM437"?>
<Person xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://
www.w3.org/2001/XMLSchema">
<FirstName>Rich</FirstName>
<LastName xsi:type="xsd:string">Blewett</LastName>
</Person>

Regards

Richard Blewett - DeveloopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk
Nov 17 '05 #4
And BTW, adding a Serializable attribute with no parameters is unnecessary.
If a class can be serialized, the SmlSerializer will serialize it.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
A watched clock never boils.

"Spidey" <sa*************@gmail.com> wrote in message
news:11*********************@g43g2000cwa.googlegro ups.com...
A class must have a default constructor to be serialized by
XmlSerializer. That is probably the reason why you are getting the
InvalidOperationException

Regards,
Sarin

Nov 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Lars-Erik Aabech | last post by:
Hi! I've got problems with serializing my collections of business objects. The objects themselves serialize fine, but the collections fail. I've got the following structure: Base collection...
5
by: David Sworder | last post by:
Hi, I've created a UserControl-derived class called MyUserControl that is able to persist and subsequently reload its state. It exposes two methods as follows: public void Serialize(Stream...
10
by: Dan | last post by:
All I Am Attempting To Serialize An Object To An XML File. Here Is The Code For That public string SaveNewSurvey( MutualSurveyObject mso_TempObject, int i_JobID ) { string s_RootFileName;...
3
by: Jerry | last post by:
Hi, I have a class like the following: class A { private B _b; A (B b) { _b = b; } ...
1
by: ALI-R | last post by:
I have two different classes as follow and in my application I create two diiferent objects of them and I want to srialize those two objects into one XML file because they are related ,Is there an...
5
by: Brad | last post by:
I would like to serialize an arraylist of objects to xml so I can store the xml in a database column. How would I code the serializing and deserializing? Below is a (overly) simple, incomplete...
0
by: John Manion via .NET 247 | last post by:
Long Post, thanks for your patience... I have and XML file that looks something like this: <?xml version="1.0" encoding="utf-8" ?> <Settings> <Location> <X>30</X> <Y>40</Y> </Location>...
1
by: Rick Luckwell | last post by:
I have 3 collections(reports, services, charts) of objects(report, service, chart) that are nested with each other. When I serialize the object the output only contains reports and services but...
1
by: davebaranas | last post by:
I am able to serialize this but I get a null exception when I try to deserialize it back Even if I don't make any child classes it throws "Object reference not set to an instance of an object."...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.