using System;
using System.IO;
using System.Xml.Serialization;
namespace RGD.Common
{
public class XmlSerialization
{
/// 反序列化
/// 反序列化
///
/// 类型
/// XML字符串
///
public static T DeSerialize(string xml)
{
try
{
using (StringReader sr = new StringReader(xml))
{
XmlSerializer xmldes = new XmlSerializer(typeof(T));
return (T)xmldes.Deserialize(sr);
}
}
catch (Exception e)
{
return default(T);
}
}
/// 序列化XML文件
/// 序列化XML文件
///
/// 类型
/// 对象
///
public static string Serializer(T t)
{
MemoryStream Stream = new MemoryStream();
//创建序列化对象
XmlSerializer xml = new XmlSerializer(typeof(T));
try
{
//序列化对象
xml.Serialize(Stream, t);
}
catch (InvalidOperationException)
{
throw;
}
Stream.Position = 0;
StreamReader sr = new StreamReader(Stream);
string str = sr.ReadToEnd().Replace(" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"", string.Empty);
return str;
}
}
}