I have the following method to save an Object to a file:
// Save an object out to the disk public static void SerializeObject<T>(this T toSerialize, String filename) { XmlSerializer xmlSerializer = new XmlSerializer(toSerialize.GetType()); TextWriter textWriter = new StreamWriter(filename); xmlSerializer.Serialize(textWriter, toSerialize); textWriter.Close(); }
I confess I did not write it (I only converted it to a extension method that took a type parameter).
Now I need it to give the xml back to me as a string (rather than save it to a file). I am looking into it, but I have not figured it out yet.
I thought this might be really easy for someone familiar with these objects. If not I will figure it out eventually.
To serialize an object to a string, we can use the base 64 encodings. We can implement the serialization by creating two classes, one class will implement the Serializable class, and the other class will be used to create the object of the Serializable class and serialize it.
The Serializable interface must be implemented by the class whose object needs to be persisted. The String class and all the wrapper classes implement the java. io. Serializable interface by default.
Use a StringWriter
instead of a StreamWriter
:
public static string SerializeObject<T>(this T toSerialize) { XmlSerializer xmlSerializer = new XmlSerializer(toSerialize.GetType()); using(StringWriter textWriter = new StringWriter()) { xmlSerializer.Serialize(textWriter, toSerialize); return textWriter.ToString(); } }
Note, it is important to use toSerialize.GetType()
instead of typeof(T)
in XmlSerializer constructor: if you use the first one the code covers all possible subclasses of T
(which are valid for the method), while using the latter one will fail when passing a type derived from T
. Here is a link with some example code that motivate this statement, with XmlSerializer
throwing an Exception
when typeof(T)
is used, because you pass an instance of a derived type to a method that calls SerializeObject that is defined in the derived type's base class: http://ideone.com/1Z5J1.
Also, Ideone uses Mono to execute code; the actual Exception
you would get using the Microsoft .NET runtime has a different Message
than the one shown on Ideone, but it fails just the same.
Serialize and Deserialize XML/JSON (SerializationHelper.cs
):
using Newtonsoft.Json; using System.IO; using System.Xml.Serialization; namespace MyProject.Helpers { public static class SerializationHelper { public static T DeserializeXml<T>(this string toDeserialize) { XmlSerializer xmlSerializer = new XmlSerializer(typeof(T)); using (StringReader textReader = new StringReader(toDeserialize)) { return (T)xmlSerializer.Deserialize(textReader); } } public static string SerializeXml<T>(this T toSerialize) { XmlSerializer xmlSerializer = new XmlSerializer(typeof(T)); using (StringWriter textWriter = new StringWriter()) { xmlSerializer.Serialize(textWriter, toSerialize); return textWriter.ToString(); } } public static T DeserializeJson<T>(this string toDeserialize) { return JsonConvert.DeserializeObject<T>(toDeserialize); } public static string SerializeJson<T>(this T toSerialize) { return JsonConvert.SerializeObject(toSerialize); } } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With