|
using System.IO;
using System.Xml.Serialization;
namespace SparkAPI.Common.Serialization
{
/// <summary>
/// Provides generic methods to save and load a serializable object to and from XML
/// </summary>
/// <typeparam name="T">Object type to be serialized</typeparam>
public class XmlSerializer<T> where T : class, new()
{
/// <summary>
/// Reads and deserializes object from specified xml data text
/// </summary>
/// <param name="xml">XML text</param>
public static T DeserializeFromString(string xml)
{
T result;
if (string.IsNullOrEmpty(xml))
{
result = new T();
}
else
{
XmlSerializer serializer = new XmlSerializer(typeof(T));
using (StringReader reader = new StringReader(xml))
{
result = (T)serializer.Deserialize(reader);
}
}
return result;
}
/// <summary>
/// Reads and deserializes object from specified file name
/// </summary>
/// <param name="fileName">Schema list file name</param>
public static T DeserializeFromFile(string fileName)
{
T result = null;
if (System.IO.File.Exists(fileName))
{
string xmlData = System.IO.File.ReadAllText(fileName);
result = DeserializeFromString(xmlData);
}
return result;
}
/// <summary>
/// Serializes and saves object to specified file name
/// </summary>
/// <param name="item"></param>
/// <param name="fileName"></param>
public static void SerializeToFile(T item, string fileName)
{
System.IO.FileStream fileStream = new System.IO.FileStream(fileName, System.IO.FileMode.Create);
System.Xml.Serialization.XmlSerializer sr = new System.Xml.Serialization.XmlSerializer(item.GetType());
sr.Serialize(fileStream, item);
fileStream.Close();
}
/// <summary>
/// Serializes and saves object to specified file name
/// </summary>
/// <param name="item"></param>
public static string SerializeToString(T item)
{
StringWriter writer = new StringWriter();
System.Xml.Serialization.XmlSerializer sr = new System.Xml.Serialization.XmlSerializer(item.GetType());
sr.Serialize(writer, item);
return writer.ToString();
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
Paul Francis currently works as a senior engineer at The Trade Desk.
He holds an undergraduate Honours degree in Finance, and is near completion of a Ph.D. in Market Microstructure, specialising in order flow modelling, and market data processing, reconstruction and analytics.
He is also the creator of Sharp Spark (Spark API SDK), an open source component designed to facilitate the processing of real-time market data from the Spark API:
http://sourceforge.net/projects/sparkapi
Paul lives in Sydney, Australia.