public static void SerializeXml(object value)
{
using (FileStream fs =
new FileStream("XmlSerializer.xml", FileMode.Create))
{
XmlSerializer serializer = new XmlSerializer(value.GetType());
serializer.Serialize(fs, value);
}
}
public static T DeserializeXml<T>()
{
using (FileStream fs =
new FileStream("XmlSerializer.xml", FileMode.Open))
{
XmlSerializer serializer = new XmlSerializer(typeof(T));
object deserializedValue = serializer.Deserialize(fs);
return (T)deserializedValue;
}
}
internal static void SerializeDataContractXml(object value)
{
using (FileStream fs =
new FileStream("DataFileDContract.xml", FileMode.Create))
using (XmlDictionaryWriter writer =
XmlDictionaryWriter.CreateTextWriter(fs))
{
DataContractSerializer serializer =
new DataContractSerializer(value.GetType());
serializer.WriteObject(writer, value);
}
}
internal static T DerializeDataContractXml<T>()
{
T value;
using (FileStream fs = new
FileStream("DataFileDContract.xml", FileMode.Open))
using (XmlDictionaryReader reader =
XmlDictionaryReader.CreateDictionaryReader(XmlReader.Create(fs)))
{
DataContractSerializer deserializer =
new DataContractSerializer(typeof(T));
value = (T)deserializer.ReadObject(reader);
}
return value;
}
internal static void SerializeDataContractBin(object value)
{
using (FileStream fs = new FileStream("DataFileDContractBin.xml", FileMode.Create))
using (XmlDictionaryWriter writer =
XmlDictionaryWriter.CreateBinaryWriter(fs))
{
DataContractSerializer serializer =
new DataContractSerializer(value.GetType());
serializer.WriteObject(writer, value);
}
}
internal static T DerializeDataContractBin<T>()
{
T value;
using (FileStream fs = new FileStream("DataFileDContractBin.xml", FileMode.Open))
using (XmlDictionaryReader reader =
XmlDictionaryReader.CreateBinaryReader(fs, XmlDictionaryReaderQuotas.Max))
{
DataContractSerializer deserializer =
new DataContractSerializer(typeof(T));
value = (T)deserializer.ReadObject(reader);
}
return value;
}
internal static void SerializeBin(object item)
{
FileStream fs = new FileStream("DataFileBin.txt", FileMode.Create);
BinaryFormatter formatter = new BinaryFormatter();
try
{
formatter.Serialize(fs, item);
}
catch (SerializationException e)
{
Console.WriteLine("Failed to serialize. Reason: " + e.Message);
throw;
}
finally
{
fs.Close();
}
}
internal static T DeserializeBin<T>()
{
T value;
FileStream fs = new FileStream("DataFileBin.txt", FileMode.Open);
try
{
BinaryFormatter formatter = new BinaryFormatter();
value = (T)formatter.Deserialize(fs);
return value;
}
catch (SerializationException e)
{
Console.WriteLine("Failed to deserialize.Reason: "
+ e.Message);
throw;
}
finally
{
fs.Close();
}
}