Posts with tag: Serialization

Here is a little utility class I wrote a in order to convert any C# class to a string and back again. The serialzied string can be saved to disk, or sent over the network to be deserialized some where else. The usage is quite straight forward.

To serialize an object to a string do this:

string serializedData = XMLDataSerializer.SerializeToString(objectToSerialize);

and to deserialize an object to a specific type use: 

string object = DataSerializer.DeSerializeFromString<ObjectType>(serializedData ); 

Here is a unit test demonstrating:

[TestMethod]
    public void SerializeToStringTest()
    {
        var testClass = new TestClass()
        {
            testint = 12345,
            teststring = "SyntaxWarriors XML Serializer"
        };
        var dataString = DataSerializer.SerializeToString(testClass);
        var testClassDeserialized = DataSerializer.DeSerializeFromString<TestClass>(dataString);

        Assert.IsTrue(testClass.testint == testClassDeserialized.testint);
        Assert.IsTrue(testClass.teststring == testClassDeserialized.teststring);
}

This is the value of dataString in the unit test.

<TestClass>
    <testint>12345</testint>
    <teststring>SyntaxWarriors XML Serializer</teststring>
</TestClass>

And here is the class itself:

using System;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
 
namespace SyntaxWarriors.Serializers
{
    internal static class XMLDataSerializer
    {
        public static string SerializeToString(object obj)
        {
            return SerializeToXml(obj);
        }
 
        public static T DeSerializeFromString<T>(string input)
        {
            T obj = DeserializeFromXml<T>(input);
            return obj;
        }

        private static string SerializeToXml<T>(T obj)
        {
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Indent = true;
            settings.OmitXmlDeclaration = true;
            settings.NewLineChars = Environment.NewLine;
            settings.NewLineHandling = NewLineHandling.None;
            settings.Encoding = Encoding.UTF8;
            using (var stringWriter = new StringWriter())
            {
                using (var xmlWriter = XmlWriter.Create(stringWriter, settings))
                {
                    var namespaces = new XmlSerializerNamespaces();
                    namespaces.Add(string.Empty, string.Empty);

                    var serializer = new XmlSerializer(obj.GetType());
                    serializer.Serialize(xmlWriter, obj, namespaces);
 
                    var xmlStr = stringWriter.ToString();
                    return xmlStr;
                }
            }
        }
 
        private static T DeserializeFromXml<T>(string xml)
        {
            T result;
            var ser = new System.Xml.Serialization.XmlSerializer(typeof(T));
            using (var tr = new System.IO.StringReader(xml))
            {
                result = (T)ser.Deserialize(tr);
            }
            return result;
        }
    }
}