C# Object Serialization to XML file


public class XmlSerializationManager
{
public static void SaveObjectToXmlFile(String path, Object _object, Type _type)
{
FileStream stream = File.Open(path, FileMode.Create, FileAccess.Write);

XmlSerializer serializer = new XmlSerializer(_type);
serializer.Serialize(stream, _object);

stream.Close();
}

public static void SaveObjectToXmlFile(String path, Object _object, Type _type, Type[] extraTypes)
{
FileStream stream = File.Open(path, FileMode.Create, FileAccess.Write);

XmlSerializer serializer = new XmlSerializer(_type, extraTypes);
serializer.Serialize(stream, _object);

stream.Close();
}

public static Object LoadObjectFromXmlFile(String path, Type _type)
{
if (!File.Exists(path))
{
return null;
}

FileStream stream = File.Open(path, FileMode.Open, FileAccess.Read);

if (stream.Length == 0)
{
return null;
}

XmlSerializer serializer = new XmlSerializer(_type);
Object _object = serializer.Deserialize(stream);

stream.Close();

return _object;
}

public static Object LoadObjectFromXmlFile(String path, Type _type, Type[] extraTypes)
{
if (!File.Exists(path))
{
return null;
}

FileStream stream = File.Open(path, FileMode.Open, FileAccess.Read);

if (stream.Length == 0)
{
return null;
}

XmlSerializer serializer = new XmlSerializer(_type, extraTypes);
Object _object = serializer.Deserialize(stream);

stream.Close();

return _object;
}
}