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

Serialize a class having property that return object as return type

P: n/a
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
Share this Question
Share on Google+
4 Replies


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

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

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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.