码迷,mamicode.com
首页 > 其他好文 > 详细

对象序列化XML

时间:2016-05-21 16:01:31      阅读:139      评论:0      收藏:0      [点我收藏+]

标签:

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

对象序列化XML

标签:

原文地址:http://www.cnblogs.com/wu-peng/p/5514836.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!