using System;
using System.Data.SqlTypes;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
namespace utility
{
/// <summary>
/// Xml序列化与反序列化
/// </summary>
public class XmlUtil
{
/// <summary>
/// 反序列化
/// </summary>
/// <param name="xml">XML字符串</param>
/// <returns></returns>
public static T Deserialize<T>(string xml) where T : new()
{
try
{
using (Stream sr = new FileStream(xml, FileMode.Open))
{
XmlSerializer xmldes = new XmlSerializer(typeof(T));
return (T)xmldes.Deserialize(sr);
}
}
catch
{
return default(T);
}
}
/// <summary>
/// 序列化
/// </summary>
/// <param name="obj">对象</param>
/// <returns></returns>
public static string Serializer<T>(T obj)
{
using (MemoryStream Stream = new MemoryStream())
{
XmlSerializer xml = new XmlSerializer(typeof(T));
try
{
//序列化对象
xml.Serialize(Stream, obj);
}
catch (InvalidOperationException)
{
throw;
}
Stream.Position = 0;
using (StreamReader sr = new StreamReader(Stream))
{
return sr.ReadToEnd();
}
}
}
}
}