}
public static string Obj2Xml(object obj)
{
string xmlStr = "";
try
{
using (MemoryStream ms = new MemoryStream())
{
XmlSerializer xml = new XmlSerializer(obj.GetType());
xml.Serialize(ms, obj);
byte[] arr = ms.ToArray();
xmlStr = Encoding.UTF8.GetString(arr);
}
}
catch { }
return xmlStr;
}
public static object Xml2Obj(string str, Type t)
{
if (str == "")
return null;
object obj = null;
try
{
using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(str)))
{
//XmlSerializer xml = XmlSerializer.FromTypes(new[] { t }).FirstOrDefault(); ;
XmlSerializer xml = new XmlSerializer(t);
obj = xml.Deserialize(ms);
}
}
catch { }
return obj;
}
原文地址:http://blog.csdn.net/xiaoweiserver/article/details/44177159