将实体对象转换成XML
using System;
using ;
using ;
namespace YG._CommonFunctions
{
/// <summary>
/// 将实体对象转换成XML
/// </summary>
public static class Class2XML
{
/// <typeparam name="T">实体类型</typeparam>
/// <param name="obj">实体对象</param>
public static string ToXml<T>(T obj)
{
try
{
MemoryStream stream = new MemoryStream();
XmlSerializer xmlSer = new XmlSerializer(typeof(T));
(stream, obj);
= 0;
StreamReader sr = new StreamReader(stream);
return ();
}
catch (Exception ex)
{
throw new Exception("将实体对象转换成XML异常", ex);
}
}
/// <summary>
/// 将XML转换成实体对象
/// </summary>
/// <typeparam name="T">实体类型</typeparam>
/// <param name="strXML">XML</param>
public static T ToClass<T>(string strXML) where T : class
{
try
{
using StringReader sr = new StringReader(strXML);
XmlSerializer serializer = new XmlSerializer(typeof(T));
return (sr) as T;
}
catch (Exception ex)
{
throw new Exception("将XML转换成实体对象异常", ex);
}
}
}
}