Tag Archive for serialization

Xml Config Entry to Write out Temporary Assemblies Created for the Serializers

<system.diagnostics>
<switches>
<add name="XmlSerialization.Compilation" value="4"/>
</switches>
</system.diagnostics>

source

Xml Config Snippet For XMLSerializer Temporary Classes

<system.diagnostics>
<switches>
<add name="XmlSerialization.Compilation" value="4"/>
</switches>
</system.diagnostics>

source

Serializing and Deserializing a Class Created with XSD.Exe Using XML Strings

public static string SerializeToXmlString(object targetInstance)
{
string retVal = string.Empty;
TextWriter writer = new StringWriter();
XmlSerializer serializer = new XmlSerializer(targetInstance.GetType());
serializer.Serialize(writer, targetInstance);
retVal = writer.ToString();
return retVal;
}
public static object DeserializeFromXmlString(string objectXml, Type targetType)
{
object retVal = null;
XmlSerializer serializer = new XmlSerializer(targetType);
StringReader stringReader = new StringReader(objectXml);
XmlTextReader xmlReader = new XmlTextReader(StringReader);
retVal = serializer.Deserialize(xmlReader);
return retVal;
}

source

Get XML String Out of an Instance of a Class Generated from an XSD File by XSD.exe

TextWriter writer = new StringWriter();
XmlSerializer serializer = new XmlSerializer(myObject.GetType());
serializer.Serialize(writer, myObject);
return writer.ToString();

source

Object Serialization

namespace MyObjSerial
{
[Serializable()]    //Set this attribute to all the classes that want to serialize
public class Employee : ISerializable //derive your class from ISerializable
{
public int EmpId;
public string EmpName;

//Default constructor
public Employee()
{
EmpId = 0;
EmpName = null;
}
}

//Deserialization constructor.
public Employee(SerializationInfo info, StreamingContext ctxt)
{
//Get the values from info and assign them to the appropriate properties
EmpId = (int)info.GetValue("EmployeeId", typeof(int));
EmpName = (String)info.GetValue("EmployeeName", typeof(string));
}

//Serialization function.
public void GetObjectData(SerializationInfo info, StreamingContext ctxt)
{
//You can use any custom name for your name-value pair. But make sure you
// read the values with the same name. For ex:- If you write EmpId as "EmployeeId"
// then you should read the same with "EmployeeId"
info.AddValue("EmployeeId", EmpId);
info.AddValue("EmployeeName", EmpName);
}
}

// Serialize Sample
Stream stream = File.Open("EmployeeInfo.osl", FileMode.Create);
BinaryFormatter bformatter = new BinaryFormatter();

Console.WriteLine("Writing Employee Information");
bformatter.Serialize(stream, mp);
stream.Close();

// Deserialize Sample
mp = null;

//Open the file written above and read values from it.
stream = File.Open("EmployeeInfo.osl", FileMode.Open);
bformatter = new BinaryFormatter();

Console.WriteLine("Reading Employee Information");
mp = (Employee)bformatter.Deserialize(stream);
stream.Close();

Console.WriteLine("Employee Id: {0}",mp.EmpId.ToString());
Console.WriteLine("Employee Name: {0}",mp.EmpName);

source