470,566 Members | 1,516 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,566 developers. It's quick & easy.

Custom serialization object type in C# throws exception


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(){

public object Vehicl


return _vehicle


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

// TODO: Add ParkingSpot.GetObjectData implementatio

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;


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

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
Nov 16 '05 #1
1 2480
1)You don't need to implement ISerializable or use the [Serializable]
attribute for XML serialization. ISerializable and the Serializable
attribute are for full-fidelity runtime serialization. XML serialization is
for the most part concerned with an object's public shape.

2) Doesn't Car need to be a subclass of Vehicle in your example?

3) [XmlElement] and [XmlAnyElement] won't help.

4) When you XML {serialize|deserialize} objects with polymorphic fields, you
must take steps to ensure that the serializer is aware of the additional
types that might be present. Given:

public class ParkingSpot
public Vehicle;


class Vehicle {...}
class Car: Vehicle {...}

One approach is to add an XmlInclude attribute to the Vehicle base class,
like this:
public class Vehicle

Alternatively, you can attribute the ParkingSpot class:
public class ParkingSpot { }

Another method is to pass an array of additional types to the serializer
when it's constructed, like so:
XmlSerializer s = new XmlSerializer(typeof(ParkingSpot),
new Type[] {typeof(Car)});

Mickey Williams
Author, "Microsoft Visual C# .NET Core Reference", MS Press

Nov 16 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Craig Schneider | last post: by
1 post views Thread by Opa | last post: by
3 posts views Thread by AnkitAsDeveloper [Ankit] | last post: by
1 post views Thread by leodippolito | last post: by
3 posts views Thread by Zachary Turner | last post: by
reply views Thread by JosAH | last post: by
1 post views Thread by livre | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.