This commit is contained in:
帅凯 贾
2023-07-18 11:18:52 +08:00
parent 53c4b66485
commit ccbca85c55
13 changed files with 504 additions and 151 deletions

View File

@ -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);
}
}
}
}