c# Xml序列化
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;
namespace Entity
{
internal class XmlHelper
{
public static void serialize_to_xml(string path, object obj)
{
XmlSerializer serializer = new XmlSerializer(obj.GetType());
string content = string.Empty;
using (StringWriter writer = new StringWriter())
{
serializer.Serialize(writer, obj);
content = writer.ToString();
}
using (StreamWriter stream_writer = new StreamWriter(path))
{
stream_writer.Write(content);
}
}
public static object deserialize_from_xml(string path, Type object_type)
{
XmlSerializer serializer = new XmlSerializer(object_type);
using (StreamReader reader = new StreamReader(path))
{
var a = serializer.Deserialize(reader);
return a;
}
}
}
}