Help with custom Serialization

G

Guest

Hi

I'm having problem serialization an object instance which contains a public property on the object type
My object hierarchy is many levels deep, so for simplicty I created to following which produces the same error

Let's say there is a class called ParkingSpot with a public member Vehicle having an object type of object
For simplicity, let's say that Vehicle could be anything, hence I create a new object of Car typ
and assign it to the Vehicle property of the ParkingSpot object

When I try to serialize the ParkingSpot class, I get an error with an inner exception stating
_innerException: {"The type SerializationTest.Car was not expected. Use the XmlInclude or SoapInclude attribute to specify types that are not known statically."

My classes are the following

using System
using System.Runtime.Serialization
using System.Xml.Serialization

public class ParkingSpot : ISerializable

private object _vehicle
public ParkingSpot(){

[XmlElement("Vehicle")
public object Vehicl

ge

return _vehicle

se

_vehicle = value


#region ISerializable Member
public void GetObjectData(SerializationInfo info, StreamingContext context

// TODO: Add ParkingSpot.GetObjectData implementatio

#endregio


public class Ca

public string Make
public string Model
public Car(){



I have a button on a form with the following serialization code

private void btnSerialize_Click(object sender, System.EventArgs e

ParkingSpot parkingSpot = new ParkingSpot()
Car car = new Car()
car.Make = "Nissan"
car.Model = "Maxima"
parkingSpot.Vehicle = car;

tr

// Serializatio
XmlSerializer s = new XmlSerializer(typeof(ParkingSpot))
TextWriter w = new StreamWriter(@"C:\ParkingSpot.xml")
s.Serialize(w, parkingSpot)
w.Close()

catch (Exception exp

Console.WriteLine ("{0}",exp.Message);



Does anyone know what the problem is
I tried changing the [XmlElement] attribute to [XmlAnyElement] attribute, but this causes another error

Please help, I've been stumped on this one for over a day

Thanks a lot
 
N

Nicholas Paldino [.NET/C# MVP]

Opa,

Basically, because you are exposing object (and the XmlSerializer only
knows about public properties, and object doesn't have any), the serializer
doesn't know how to deal with it. If you want your object serialized, then
you have to use the SoapFormatter, or you have to strongly type the
property.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Opa said:
Hi,

I'm having problem serialization an object instance which contains a
public property on the object type.
My object hierarchy is many levels deep, so for simplicty I created to
following which produces the same error:
Let's say there is a class called ParkingSpot with a public member Vehicle
having an object type of object.
For simplicity, let's say that Vehicle could be anything, hence I create a new object of Car type
and assign it to the Vehicle property of the ParkingSpot object.

When I try to serialize the ParkingSpot class, I get an error with an inner exception stating:
_innerException: {"The type SerializationTest.Car was not expected. Use
the XmlInclude or SoapInclude attribute to specify types that are not known
statically." }
My classes are the following:

using System;
using System.Runtime.Serialization;
using System.Xml.Serialization;

public class ParkingSpot : ISerializable
{
private object _vehicle;
public ParkingSpot(){}

[XmlElement("Vehicle")]
public object Vehicle
{
get
{
return _vehicle;
}
set
{
_vehicle = value;
}
}
#region ISerializable Members
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
// TODO: Add ParkingSpot.GetObjectData implementation
}
#endregion
}


public class Car
{
public string Make;
public string Model;
public Car(){}

}


I have a button on a form with the following serialization code:

private void btnSerialize_Click(object sender, System.EventArgs e)
{
ParkingSpot parkingSpot = new ParkingSpot();
Car car = new Car();
car.Make = "Nissan";
car.Model = "Maxima";
parkingSpot.Vehicle = car;

try
{
// Serialization
XmlSerializer s = new XmlSerializer(typeof(ParkingSpot));
TextWriter w = new StreamWriter(@"C:\ParkingSpot.xml");
s.Serialize(w, parkingSpot);
w.Close();
}
catch (Exception exp)
{
Console.WriteLine ("{0}",exp.Message);
}
}


Does anyone know what the problem is?
I tried changing the [XmlElement] attribute to [XmlAnyElement] attribute, but this causes another error.

Please help, I've been stumped on this one for over a day.

Thanks a lot.
 

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