XML serialization and de-serialization Helper class using Generics
I was doing my now and again constructing on the fly class libraries :), and while this doesn't save time it sure does help in refining some previously pretty badly written code and smash some ill-conceived ideas.
Here is an XML serialization helper class that will serialize & deserialize any object that abides by the XML serialization rules in .NET. Don't forget to check out Saving objects as XML to cookies using the Serialization Helper class article.
Usings
using System; using System.Collections.Generic; using System.IO; using System.Xml.Serialization;
XmlSerializationHelper class
/// <summary>
/// Performs XML serialization & deserialization
/// on objects that satisfy the XML serialization
/// requirements. Intended to be high performing
/// without going overboard.
/// </summary>
public static class XmlSerializerHelper
{
/// <summary>
/// Maintains a list of XML formatters
/// </summary>
private static readonly Dictionary<Type, XmlSerializer> XmlFormatter;
static XmlSerializerHelper()
{
XmlFormatter = new Dictionary<Type, XmlSerializer>();
}
/// <summary>
/// Gets the formatter for the specified type. If
/// the formatter is not available it is created.
/// </summary>
private static XmlSerializer GetFormatter(Type objType)
{
if (!XmlFormatter.ContainsKey(objType))
XmlFormatter.Add(objType, new XmlSerializer(objType));
return XmlFormatter[objType];
}
/// <summary>
/// Serializes the object to XML
/// </summary>
public static string ToXml<T>(T obj)
where T : new()
{
using (StringWriter sw = new StringWriter())
{
GetFormatter(obj.GetType()).Serialize(sw, obj);
return sw.ToString();
}
}
/// <summary>
/// Deserializes the object from XML
/// </summary>
public static T FromXml<T>(string objectAsXml)
where T : new()
{
if (!String.IsNullOrEmpty(objectAsXml))
{
using (StringReader sr = new StringReader(objectAsXml))
{
return (T) GetFormatter(typeof(T)).Deserialize(sr);
}
}
return default(T);
}
}
This boils down to a short, simple and sweet XML serialization helper class. Here is an example of this class in action, Saving objects as XML to cookies using the Serialization Helper class.
Hi there Jackson, if you are a new web user then you must pay a quick visit all the time this website and read the updated posts at here.
I'd pretty much like to see such a seiers from you indeed, mate.First reason is that such a thing really would provide compressed insight into various subjects every programmer runs into sooner or later.Second reason is there's never such a thing as too much information on a subject.And third reason is that I personally struggled with especially VFS for a long time and I'm curious on other people's solutions, so I can compare em to mine and see where I can adapt and beautify my solution there :)
Hi there Jackson, if you are a new internet user afterward you have to pay a visit every day this site and read the updated posts at at this place.
It depends on the pvperectise.The scala code itself, though the plugin gave me an hard time, was still a joy to write.The real issue lies with java code using the scala code (ie more than 99% of our codebase).For my team to discover and use any code I can write in scala, I really need autocompletion to work flawlessly.That point is totally blocking.After that, there are silent failures like the various refactoring not even noticing they should affect scala code.David
Hello! dgggeeb interesting dgggeeb site! I'm really like it! Very, very dgggeeb good!
Hi would you mind stating which blog platform you're using? I'm going to start my own blog soon but I'm having a difficult time selecting between BlogEngine/Wordpress/B2evolution and Drupal. The reason I ask is because your design and style seems different then most blogs and I'm looking for something completely unique. P.S Apologies for getting off-topic but I had to ask!