/// <summary>
/// 对象序列化XML
/// </summary>
/// <param name="type">类型</param>
/// <param name="obj">对象</param>
/// <returns></returns>
public static string Serializer<T>(T obj)
{
MemoryStream Stream = new MemoryStream();
XmlSerializer xml = new XmlSerializer(typeof(T));
try
{
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("", "");
//序列化对象
xml.Serialize(Stream, obj, ns);
}
catch (InvalidOperationException)
{
throw;
}
Stream.Position = 0;
StreamReader sr = new StreamReader(Stream);
string str = sr.ReadToEnd();
sr.Dispose();
Stream.Dispose();
return str;
}
时间: 2024-10-10 14:31:52