对象序列化XML

/// <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;
}

上一篇:final修饰符(5)-final方法


下一篇:[转帖]gesture recognition