Generics Serializer with file saving on file system and in String
using System;
using System.Xml.Serialization;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace Retalix.Client.HandHeldTraderJoesOffice.Model
{
public class SerializerUtils
{
public static object Deserialize<T>(string serializeableFilePath)
{
var serializer = new XmlSerializer(typeof(T));
var reader = new StreamReader(serializeableFilePath);
T result = (T)serializer.Deserialize(reader);
reader.Close();
return result;
}
public static object Serialize(T data, string serializeableFilePath)
{
var serializer = new XmlSerializer(typeof(T));
var writer = new StreamWriter(serializeableFilePath);
serializer.Serialize(writer, data);
writer.Close();
return result;
}
}
}
-----------------------------------------------------------------------------------------------------------------
using System;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
using BroadwayBox.SeatingChart.Common;
namespace BroadwayBoxSeattingChartWS.Serialization
{
public class SerializerUtils
{
public static string Serialize<T>(T data)
{
try
{
var serializer = new XmlSerializer(typeof(T));
TextWriter writer = new StringWriter();
serializer.Serialize(writer, data);
return writer.ToString();
}
catch (Exception exception)
{
return string.Empty;
}
}
public static T Deserialize<T>(string data)
{
var serializer = new XmlSerializer(typeof(T));
TextReader reader = new StringReader(data);
return (T)serializer.Deserialize(reader);
}
public static string SerializeWithXmlTextWriter(Chart chart)
{
var ms = new MemoryStream();
using (var writer = new XmlTextWriter(ms, Encoding.UTF8))
{
chart.WriteToXml(writer);
return Encoding.UTF8.GetString(ms.ToArray());
}
}
}
}
No comments:
Post a Comment