Serialize a class having property that return object as return type


S

surindersaini

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;}
}
}
 
Ad

Advertisements

S

Spidey

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
 
N

Nicholas Paldino [.NET/C# MVP]

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.
 
R

Richard Blewett [DevelopMentor]

Spidey said:
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
 
Ad

Advertisements

K

Kevin Spencer

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.
 

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