using System;
using System.IO;
using System.Xml.Serialization;
namespace LaserScanMicrometer
{
/// <summary>
/// 序列化相关
/// </summary>
internal class XML
{
/// <summary>
/// 序列化
/// </summary>
/// <returns> </returns>
public bool Serialize(object obj, string filePath)
{
if (obj != null)
{
try
{
var formatter = new XmlSerializer(obj.GetType());
using (var stream = new FileStream(filePath, FileMode.Create))
{
formatter.Serialize(stream, obj);
}
}
catch (Exception e)
{
return false;
}
}
return true;
}
/// <summary>
/// 反序列化
/// </summary>
/// <param name = "type"> </param>
/// <param name = "filePath"> </param>
/// <returns> </returns>
public object Deserialize(Type type, string filePath)
{
try
{
var formatter = new XmlSerializer(type);
using (var stream = new FileStream(filePath, FileMode.Open))
{
return formatter.Deserialize(stream);
}
}
catch (Exception ex)
{
return null;
}
}
}
}