XML Serialization Helpers

public static class Serialization {
private static readonly Encoding _defaultEncoding = Encoding.UTF8;

public static T DeserializeXmlFile<T>(string fileName) {
if (!File.Exists(fileName))
throw new FileNotFoundException();

var serializer = new XmlSerializer(typeof(T));
using (var xmlReader = new XmlTextReader(fileName)) {
return (T)serializer.Deserialize(xmlReader);

public static T Deserialize<T>(TextReader textReader) {
if (textReader == null)
throw new ArgumentNullException("textReader");

var serializer = new XmlSerializer(typeof(T));
return (T)(serializer.Deserialize(textReader));

public static T Deserialize<T>(string serializedObject) {
if (string.IsNullOrEmpty(serializedObject))
return default(T);

var serializer = new XmlSerializer(typeof(T));
using (var objectReader = new StringReader(serializedObject)) {
return (T)serializer.Deserialize(objectReader);

public static void SerializeToXmlFile<T>(T objectToSerialize, string fileName) {
(objectToSerialize, fileName, _defaultEncoding);

public static void SerializeToXmlFile<T>(T objectToSerialize, string fileName, Encoding encoding) {
var serializer = new XmlSerializer(typeof(T));
using (var xmlWriter = new XmlTextWriter(fileName, encoding) { Formatting = Formatting.Indented, IndentChar = '\t', Indentation = 1 }) {
.Serialize(xmlWriter, objectToSerialize);

public static string Serialize<T>(T objectToSerialize, bool includeXmlDeclaration, bool indent) {
var serializer = new XmlSerializer(typeof(T));
var sb = new StringBuilder(0x1000);

XmlTextWriter xmlWriter
new XmlTextWriter(new StringWriter(sb))
: new XmlTextWriterWithoutDeclaration(new StringWriter(sb));

if (indent) {
.Formatting = Formatting.Indented;
.IndentChar = '\t';
.Indentation = 1;

var namespaces = new XmlSerializerNamespaces();
.Add(string.Empty, string.Empty);
.Serialize(xmlWriter, objectToSerialize, namespaces);
return sb.ToString();

private class XmlTextWriterWithoutDeclaration
: XmlTextWriter
public XmlTextWriterWithoutDeclaration(TextWriter w)
: base(w)
{ }

public override void WriteStartDocument() { }

public override void WriteStartDocument(bool standalone) { }

These are just a few helpers to make using the built-in XmlSerializer a little bit easier. It skips the needless XML namespaces, which just ugly up simple XML. If you want to skip the <?xml version=”1.0″ encoding=”utf-16″?> declaration, then you can use that overload.

[Serializable] public class Foo { public string Name { get; set; } public int Age { get; set; }

var foo = new Foo { Name = "john", Age = 34 };
string xml = Serialization.Serialize(foo, true, false); // Keep declaration
xml = Serialization.Serialize(foo, false, false); // Remove declaration
using (var writer = new StringWriter())
new XmlSerializer(typeof(Foo)).Serialize(writer, foo); // Default serialization
<?xml version="1.0" encoding="utf-16"?><Foo><Name>john</Name><Age>34</Age></Foo>


<?xml version="1.0" encoding="utf-16"?>
<Foo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s