屠宰场客户端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

47 lines
1.1 KiB

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
namespace ButcherManage.BO.Utils
{
public static class XmlUtil
{
static string config = "Config";
public static void SerializerObjToFile(object obj, string fileName = "")
{
if (string.IsNullOrWhiteSpace(fileName))
{
fileName = Path.Combine(config, obj.GetType().Name + ".xml");
}
var ser = new XmlSerializer(obj.GetType());
using (var stream = File.Open(fileName, FileMode.Create))
{
ser.Serialize(stream, obj);
}
}
public static T DeserializeFromFile<T>(string fileName = "")
where T : new()
{
if (string.IsNullOrWhiteSpace(fileName))
{
fileName = Path.Combine(config, typeof(T).Name + ".xml");
}
if (!File.Exists(fileName))
{
return new T();
}
using (var reader = new StreamReader(fileName))
{
var xs = new XmlSerializer(typeof(T));
object obj = xs.Deserialize(reader);
reader.Close();
return (T)obj;
}
}
}
}