<?xml version="1.0" encoding="UTF-8"?>
<entry>
  <category-id type="integer">1</category-id>
  <content>&lt;p&gt;Do you ever have an object lying around that you would like to save off to the database and then be able to retrieve it later as that same object type? Then this might help you. Note that it only works with class objects that are serializable, and these must have a parameterless constructor.&lt;/p&gt;
&lt;p&gt;They come in handy to serialize Generic List objects too.&lt;/p&gt;

&lt;pre name="code" class="csharp"&gt;
		public static string SerializeToString(object obj)
		{
			string output = null;
			if (obj != null)
			{
				XmlSerializer serializer = new XmlSerializer(obj.GetType());

				using (StringWriter writer = new StringWriter())
				{
					serializer.Serialize(writer, obj);

					output = writer.ToString();
				}
			}
			return output;
		}
&lt;/pre&gt;

&lt;p&gt; And then to serialize back into the object:&lt;/p&gt;
&lt;pre name="code" class="csharp"&gt;
		public static T SerializeFromString&lt;T&gt;(string xml)
		{
			XmlSerializer serializer = new XmlSerializer(typeof(T));

			using (StringReader reader = new StringReader(xml))
			{
				return (T)serializer.Deserialize(reader);
			}
		}
&lt;/pre&gt;</content>
  <created-at type="datetime">2009-03-18T17:51:12-07:00</created-at>
  <id type="integer">28</id>
  <title>Serialize to and from XML</title>
  <updated-at type="datetime">2009-03-18T17:59:16-07:00</updated-at>
</entry>
