Handy C# XML Serialization Methods

I have been hacking away on WPF/MVVM, GWT/MVP, and Rails for the last six months and haven’t found much time to update. But I wrote some handy C# XML serialization methods. They are very straightforward.

Deserialize:

public static T ReadFromFile(string file) {
	XmlSerializer xs = new XmlSerializer(typeof(T));
	StreamReader sr = new StreamReader(file);
	T ret = (T)xs.Deserialize(sr);
	sr.Close();
	return ret;
}

Serialize:

public static void WriteToFile(T obj, string file) {
	XmlSerializer xs = new XmlSerializer(typeof(T));
	StreamWriter sw = new StreamWriter(file);
	xs.Serialize(sw, obj);
	sw.Close();
}

Enjoy.

Are you a Git user? Let me help you make project management with Git simple. Checkout Gitpilot.

Advertisement

Leave a Reply

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

Gravatar
WordPress.com Logo

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

Twitter picture

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

Facebook photo

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

Connecting to %s

Follow

Get every new post delivered to your Inbox.