41 lines
1.2 KiB
C#
41 lines
1.2 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Xml.Serialization;
|
|
|
|
namespace DakotaSender
|
|
{
|
|
public static class XmlFileConverter
|
|
{
|
|
/// <summary>
|
|
/// Saves to an xml file
|
|
/// </summary>
|
|
/// <param name="FileName">File path of the new xml file</param>
|
|
public static void Save<T>(T obj, string FileName) where T : class
|
|
{
|
|
using (var writer = new System.IO.StreamWriter(FileName))
|
|
{
|
|
var serializer = new XmlSerializer(obj.GetType());
|
|
serializer.Serialize(writer, obj);
|
|
writer.Flush();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Load an object from an xml file
|
|
/// </summary>
|
|
/// <param name="FileName">Xml file name</param>
|
|
/// <returns>The object created from the xml file</returns>
|
|
public static T Load<T>(string FileName) where T : class
|
|
{
|
|
using (var stream = System.IO.File.OpenRead(FileName))
|
|
{
|
|
var serializer = new XmlSerializer(typeof(T));
|
|
return serializer.Deserialize(stream) as T;
|
|
}
|
|
}
|
|
}
|
|
}
|