Posts

Showing posts from July, 2010

C# Serialize an object to/from XML

There have been many times I've needed to serialize an object to/from XML. .NET makes this incredibly easy. With a few meta tags your class(es) can easily be serialized and deserialized. You can then take this even further any start applying custom attributes to have as much granular control over the serialization as you want.

For instance lets' take a very basic class with some data and properties.

public class Person { int _age; String _name; /// Gets or sets the Age of the person. public int Age { get { return _age; } set { _age = value; } } /// Gets or sets the Name of the person. public String Name { get { return _name; } set { _name = value; } } /// Creates a new instance of the Person class public Person() { } } Now let's take this class and add the [Serializable] attribute.

[Serializable] public class Person { int _age; String _name; /// Gets or sets the Age of the…