This commit is contained in:
@ -478,9 +478,41 @@ namespace Epost.Common
|
||||
return model;
|
||||
}
|
||||
|
||||
public static string ModelToXml<T>(T model)
|
||||
{
|
||||
MemoryStream stream = new MemoryStream();
|
||||
XmlSerializer xmlSer = new XmlSerializer(typeof(T));
|
||||
xmlSer.Serialize(stream, model);
|
||||
|
||||
stream.Position = 0;
|
||||
StreamReader sr = new StreamReader(stream);
|
||||
return sr.ReadToEnd();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将实体对象转换成XML
|
||||
/// </summary>
|
||||
/// <typeparam name="T">实体类型</typeparam>
|
||||
/// <param name="obj">实体对象</param>
|
||||
public static string XmlSerialize<T>(T obj)
|
||||
{
|
||||
try
|
||||
{
|
||||
using (StringWriter sw = new StringWriter())
|
||||
{
|
||||
Type t = obj.GetType();
|
||||
XmlSerializer serializer = new XmlSerializer(obj.GetType());
|
||||
serializer.Serialize(sw, obj);
|
||||
sw.Close();
|
||||
return sw.ToString();
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw new Exception("将实体对象转换成XML异常", ex);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user