//对象转XML
public static string ObjToXml(object obj)
{
using (MemoryStream Stream = new MemoryStream())
{
XmlSerializer xml = new XmlSerializer(obj.GetType());
xml.Serialize(Stream, obj);
Stream.Position = 0;
StreamReader sr = new StreamReader(Stream);
string str = sr.ReadToEnd();
return str;
}
}
//XML转对象
public static T XmlToObj<T>(string xml)
{
XmlSerializer xserializer = new XmlSerializer(typeof(T));
using (StringReader sr = new StringReader(xml))
{
return (T)xserializer.Deserialize(sr);
}
}