直接上代码:
public static class XmlSerializer
{
public static void SaveToXml(string filePath, object sourceObj, Type type)
{
if (!string.IsNullOrWhiteSpace(filePath) && sourceObj != null)
{
type = type != null ? type : sourceObj.GetType();
using (StreamWriter writer = new StreamWriter(filePath))
{
System.Xml.Serialization.XmlSerializer xmlSerializer = new System.Xml.Serialization.XmlSerializer(type);
XmlSerializerNamespaces nameSpace = new XmlSerializerNamespaces();
nameSpace.Add("", ""); //not ot output the default namespace
xmlSerializer.Serialize(writer, sourceObj, nameSpace);
}
}
}
public static object LoadFromXml(string filePath, Type type)
{
object result = null;
if (File.Exists(filePath))
{
using (StreamReader reader = new StreamReader(filePath))
{
System.Xml.Serialization.XmlSerializer xmlSerializer = new System.Xml.Serialization.XmlSerializer(type);
result = xmlSerializer.Deserialize(reader);
}
}
return result;
} public static class XmlSerializer