473,396 Members | 2,009 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,396 software developers and data experts.

Custom serialization object type in C# throws exception

Opa
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
Nov 16 '05 #1
1 2604
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;
}

and:

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

One approach is to add an XmlInclude attribute to the Vehicle base class,
like this:
[XmlInclude(typeof(Car))]
public class Vehicle
{
....
}

Alternatively, you can attribute the ParkingSpot class:
[XmlInclude(typeof(Car))]
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
www.neudesic.com
www.servergeek.com/blogs/mickey

Nov 16 '05 #2

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

Similar topics

0
by: Craig Schneider | last post by:
// Is there any way to override the XML Serialization of the following SimpleClass // to turn off the DefaultValue of a boolean? Sure, I can override the DefaultValue from // true to false, but...
1
by: Opa | last post by:
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...
3
by: AnkitAsDeveloper [Ankit] | last post by:
Hi i am serializing a 'ref struct' object as follows : private: void Seri( String ^path, Object^ obj ) { FileStream^ fileStrm ; try { //Serialize entire object into Binary stream
1
by: Knecke | last post by:
Hi all. I have a problem with returning a custom Result object with webservice. The classes i use is described below (some fields and properties is removed) public class Result { int...
2
by: AMDRIT | last post by:
Hello everyone, I have created a custom component and one of its properties is a class object with it's own properties. During runtime, I can assign values to the class object properties just...
1
by: leodippolito | last post by:
Dear sirs, I am using custom wrappers to primitive types in my classes, so I can have some flags when working with the database ("undefined" and "null") .. So instead of: public class...
3
by: Zachary Turner | last post by:
Hello, I have a situation where I would like to perform custom serialization and deserialization of an existing .NET framework object (specifically, System.DateTime). Is there a common paradigm...
4
by: ThunderMusic | last post by:
Hi, I have a custom form that works fine when I debug it or run it in release mode but cannot be loaded in the designer... Actually, it can be loaded in the designer when no control is on it, but...
0
by: JosAH | last post by:
Introduction Upon hearing the word, "Serialization", the first question which comes to mind is ... "What is Serialization?" We know that we can create resusable objects in Java. But the...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.