Skip to main content

Serialisieren von Objekten

Serialisierung

ToBinary

/// <summary>
/// Serializes to binary.
/// </summary>
/// <param name="value">The value.</param>
/// <returns></returns>
public static byte[] SerializeToBinary(object value)
{
    using (MemoryStream ms = new MemoryStream())
    {
        BinaryFormatter bf = new BinaryFormatter();
 
        bf.Serialize(ms, value);
 
        return ms.ToArray();
    }
}

ToBinaryFile

/// <summary>
/// Serializes to binary file.
/// </summary>
/// <param name="fileName">Name of the file.</param>
/// <param name="value">The value.</param>
public static void SerializeToBinaryFile(string fileName, object value)
{
    BinaryFormatter bf = new BinaryFormatter();
 
    using (StreamWriter sw = new StreamWriter(fileName, false))
    {
        bf.Serialize(sw.BaseStream, value);
    }
}

ToXML

/// <summary>
/// Serializes to XML.
/// </summary>
/// <param name="value">The value.</param>
/// <returns></returns>
public static string SerializeToXml(object value)
{
    if (value == null)
    {
        return string.Empty;
    }
 
    string xml = string.Empty;
 
    try
    {
        XmlSerializer ser = new XmlSerializer(value.GetType());
 
        using (MemoryStream ms = new MemoryStream())
        {
            StreamWriter sw = new StreamWriter(ms, Encoding.UTF8);
            ser.Serialize(sw, value);
            ms.Seek(0, SeekOrigin.Begin);
 
            StreamReader sr = new StreamReader(ms);
            xml = sr.ReadToEnd();
        }
    }
    catch (InvalidOperationException ioex)
    {
        throw new InvalidOperationException(ioex.Message);
    }
    catch (ArgumentNullException anex)
    {
        throw new ArgumentNullException(anex.Message);
    }
    catch (IOException ioex)
    {
        throw new IOException(ioex.Message);
    }
    catch (ArgumentOutOfRangeException aoorex)
    {
        throw new ArgumentOutOfRangeException(aoorex.Message);
    }
    catch (ArgumentException aex)
    {
        throw new ArgumentException(aex.Message);
    }
    catch (OutOfMemoryException oomex)
    {
        throw new OutOfMemoryException(oomex.Message);
    }
 
    return xml;
}

ToXMLFile

/// <summary>
/// Serializes to XML file.
/// </summary>
/// <param name="fileName">Name of the file.</param>
/// <param name="value">The value.</param>
public static void SerializeToXmlFile(string fileName, object value)
{
    if (value == null)
    {
        return;
    }
 
    try
    {
        XmlSerializer ser = new XmlSerializer(value.GetType());
 
        using (StreamWriter sw = new StreamWriter(fileName, false, Encoding.UTF8))
        {
            ser.Serialize(sw, value);
        }
    }
    catch (ArgumentNullException anex)
    {
        throw new ArgumentNullException(anex.Message);
    }
    catch (IOException ioex)
    {
        throw new IOException(ioex.Message);
    }
    catch (ArgumentOutOfRangeException aoorex)
    {
        throw new ArgumentOutOfRangeException(aoorex.Message);
    }
    catch (ArgumentException aex)
    {
        throw new ArgumentException(aex.Message);
    }
    catch (ObjectDisposedException odex)
    {
        throw new ObjectDisposedException(odex.Message);
    }
    catch (OutOfMemoryException oomex)
    {
        throw new OutOfMemoryException(oomex.Message);
    }
}

Deserialisierung

FromXML

/// <summary>
/// Deserializes from XML.
/// </summary>
/// <param name="xml">The XML.</param>
/// <param name="type">The type.</param>
/// <returns></returns>
public static object DeserializeFromXml(string xml, Type type)
{
    if (type == null)
    {
        return null;
    }
 
    try
    {
        using (StringReader sr = new StringReader(xml))
        {
            return new XmlSerializer(type).Deserialize(sr);
        }
    }
    catch (ArgumentNullException anex)
    {
        throw new ArgumentNullException(anex.Message);
    }
    catch (InvalidOperationException ioex)
    {
        throw new InvalidOperationException(ioex.Message);
    }
 
    return null;
}

FromXMLFile

/// <summary>
/// Deserializes from XML file.
/// </summary>
/// <param name="fileName">Name of the file.</param>
/// <param name="type">The type.</param>
/// <returns></returns>
public static object DeserializeFromXmlFile(string fileName, Type type)
{
    if (type == null)
    {
        return null;
    }
 
    try
    {
        using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
        {
            return new XmlSerializer(type).Deserialize(fs);
        }
    }
    catch (ArgumentNullException anex)
    {
        throw new ArgumentNullException(anex.Message);
    }
    catch (InvalidOperationException ioex)
    {
        throw new InvalidOperationException(ioex.Message);
    }
 
    return null;
}

FromBinaryFile

/// <summary>
/// Deserializes from binary file.
/// </summary>
/// <param name="fileName">Name of the file.</param>
/// <returns></returns>
public static object DeserializeFromBinaryFile(string fileName)
{
    using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
    {
        return ObjectHelper.DeserializeFromBinary(fs);
    }
}

FromBinary

/// <summary>
/// Deserializes from binary.
/// </summary>
/// <param name="serializedObject">The serialized object.</param>
/// <returns></returns>
public static object DeserializeFromBinary(byte[] serializedObject)
{
    if (serializedObject == null)
    {
        return null;
    }
 
    using (MemoryStream dataInMemory = new MemoryStream(serializedObject))
    {
        return new BinaryFormatter().Deserialize(dataInMemory);
    }
}

FromBinary (Stream)

/// <summary>
/// Deserializes from binary.
/// </summary>
/// <param name="stream">The stream.</param>
/// <returns></returns>
public static object DeserializeFromBinary(Stream stream)
{
    if (stream == null)
    {
        return null;
    }
 
    try
    {
        BinaryFormatter formatter = new BinaryFormatter();
        object obj = null;
 
        try
        {
            obj = formatter.Deserialize(stream);
        }
        finally
        {
            stream.Close();
        }
 
        return obj;
    }
    catch (ArgumentNullException anex)
    {
        throw new ArgumentNullException(anex.Message);
    }
    catch (SerializationException sex)
    {
        throw new SerializationException(sex.Message);
    }
    catch (SecurityException sex)
    {
        throw new SecurityException(sex.Message);
    }
 
    return null;
}