XmlSerializer

  • Thread starter =?iso-8859-2?Q?S=B3awomir_Krzy=BFanowski?=
  • Start date
?

=?iso-8859-2?Q?S=B3awomir_Krzy=BFanowski?=

When I execute this code everything works fine, no exception is throwed.
Method GetObjectData was not entered, but object was serialized and file
cos.xml was created with correct data. When I use BinaryFormater then
Exception(" That metod... is throwed. I don't know why GetObjectData is not
executed with XmlSerializer.

FileStream stream = new FileStream(@"d:\cos.xml", FileMode.Open,
FileAccess.ReadWrite);
XmlSerializerFactory serializerFactory = new XmlSerializerFactory();
XmlSerializer serializer =
serializerFactory.CreateSerializer(typeof(Program));
serializer.Serialize(stream, new Program());

public class Program : ISerializable
{
public Int16 age;

public void GetObjectData(SerializationInfo info, StreamingContext
context)
{
throw new Exception("The method or operation is not
implemented.");
}
}
 
N

Nicholas Paldino [.NET/C# MVP]

It's not called because the ISerializable interface is used to customize
serialization that is used with anything implementing IFormatter (which
BinaryFormatter does). However, you use the XmlSerializer, which is a
completely different serialization mechanism.

If you want to customize the serialization when using the XmlSerializer,
then you will have to implement the IXmlSerializable interface and/or use
attributes in the System.Xml.Serialization namespace, as well as use the
XmlSerializer class to deserialize your instance.
 

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