these two simple extension methods with have you switching between XML and objects in no time
First off, it is pretty well known that if you have any Object and want to convert it into an XML strong, you can use the XmlSerializer to do so. I encapsulated the process is a simple extension method that run from any object – this will work as long as the object has a constructor with no inputs (ie: new SomeObject()) as that is the constraint passed up from XmlSerializer. Here is the method for converting from an object to an xml string:
public static string ToXmlString(this Object o)
{
StringWriter xml = new
StringWriter(new StringBuilder());
XmlSerializer xS = new XmlSerializer(o.GetType());
xS.Serialize(xml, o);
return xml.ToString();
}
With this, now all you need to do is:
MyObject obj = new MyObject(); // a bunch of stuff here... // now I want the xml representation of this: string xml = obj.ToXmlString();
Now to go backwards, you can use the similar Deserialize along with the XmlSerializer with this method:
public static T XmlToObject<T>(this string s)
{
var xR = XmlReader.Create(new
StringReader(s));
XmlSerializer xS = new XmlSerializer(typeof(T));
T obj = (T)xS.Deserialize(xR);
return obj;
}
It is important to notice that this is taking in a raw xml string, which would not be web safe. If you were taking in data from a web source, you would want to employ HttpUtility.UrlDecode(s) instead of just s above.
Now if you want to turn your above string ‘xml’ into an object again, simply call it like this:
MyObject obj2 = xml.XmlToObject<SomeObject>();
A few weeks back my company offered a free online webinar on LINQ technologies to help developers more easily make the transition to LINQ. While there was a great turn out at the webinar, I received several emails from people who couldn’t attend asking if I could provide a video recording of the webinar. It turns out that the audio for the recording wasn’t up to my standards so I put together a 
Windows only: If you’re delving into XML programming for the first time, or want to tweak a few software files, Microsoft’s XML Notepad 2007 may be the no-nonsense editor you’re looking for.