You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

91 lines
1.4 KiB
C#

2 years ago
using System;
using System.Diagnostics;
using System.IO;
using System.Xml.Serialization;
namespace Rs.Framework
{
public class XmlSerializerHelper
{
private static XmlSerializerHelper instance = new XmlSerializerHelper();
private XmlSerializerHelper()
{
}
public static XmlSerializerHelper Instance
{
get
{
return XmlSerializerHelper.instance;
}
}
public bool Serialize<T>(Stream stream, T obj)
{
try
{
XmlSerializer xs = new XmlSerializer(typeof(T));
xs.Serialize(stream, obj);
return true;
}
catch (Exception ex)
{
}
return false;
}
public bool Deserialize<T>(Stream stream, out T obj)
{
obj = default(T);
try
{
XmlSerializer xs = new XmlSerializer(typeof(T));
T result = (T)((object)xs.Deserialize(stream));
obj = result;
return true;
}
catch (Exception ex)
{
}
return false;
}
public bool Serialize<T>(string sPath, T obj)
{
try
{
using (Stream s = new FileStream(sPath, FileMode.Create))
{
return this.Serialize<T>(s, obj);
}
}
catch (Exception ex)
{
}
return false;
}
public bool Deserialize<T>(string sPath, out T obj)
{
obj = default(T);
try
{
using (Stream s = new FileStream(sPath, FileMode.Open))
{
return this.Deserialize<T>(s, out obj);
}
}
catch (Exception ex)
{
}
return false;
}
}
}