<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://weblogs.asp.net/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Serializing and Deserializing Objects…to and from…XML</title><link>http://weblogs.asp.net/stevewellens/archive/2009/07/02/serializing-and-deserializing-objects-to-and-from-xml.aspx</link><description>Over on the Asp.Net forums I recently had the opportunity* to help a few lost souls by showing them how to serialize objects to XML and deserialize the XML back into objects. Since the question has come up more than once, I decided to BLOG it so I could</description><dc:language>en</dc:language><generator>CommunityServer 2007 SP1 (Build: 20510.895)</generator><item><title>re: Serializing and Deserializing Objects…to and from…XML</title><link>http://weblogs.asp.net/stevewellens/archive/2009/07/02/serializing-and-deserializing-objects-to-and-from-xml.aspx#7197269</link><pubDate>Wed, 09 Sep 2009 06:43:36 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7197269</guid><dc:creator>sirdneo</dc:creator><author>sirdneo</author><description>&lt;p&gt;I generally use extension methods to Serilize and De-Serlize objetcs to XML. Thanks for sharing&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7197269" width="1" height="1"&gt;</description></item><item><title>Serializing and Deserializing Objects…to and from…XML - Steve Wellens</title><link>http://weblogs.asp.net/stevewellens/archive/2009/07/02/serializing-and-deserializing-objects-to-and-from-xml.aspx#7140632</link><pubDate>Mon, 06 Jul 2009 10:21:03 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7140632</guid><dc:creator>DotNetShoutout</dc:creator><author>DotNetShoutout</author><description>&lt;p&gt;Thank you for submitting this cool story - Trackback from DotNetShoutout&lt;/p&gt;
&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7140632" width="1" height="1"&gt;</description></item><item><title>re: Serializing and Deserializing Objects…to and from…XML</title><link>http://weblogs.asp.net/stevewellens/archive/2009/07/02/serializing-and-deserializing-objects-to-and-from-xml.aspx#7139219</link><pubDate>Fri, 03 Jul 2009 09:46:35 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7139219</guid><dc:creator>flalar</dc:creator><author>flalar</author><description>&lt;p&gt;You could also use generics for serialization &lt;/p&gt;
&lt;p&gt;public static string Serialize&amp;lt;T&amp;gt;(T objectToSerialize, XmlSerializer serializer) where T : new()&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;StringBuilder stringBuilder = new StringBuilder();&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;using (XmlWriter xmlWriter = XmlWriter.Create(stringBuilder))&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;serializer.Serialize(xmlWriter, objectToSerialize);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;xmlWriter.Flush();&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return stringBuilder.ToString();&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7139219" width="1" height="1"&gt;</description></item><item><title>re: Serializing and Deserializing Objects…to and from…XML</title><link>http://weblogs.asp.net/stevewellens/archive/2009/07/02/serializing-and-deserializing-objects-to-and-from-xml.aspx#7139169</link><pubDate>Fri, 03 Jul 2009 08:13:35 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7139169</guid><dc:creator>Jv</dc:creator><author>Jv</author><description>&lt;p&gt;Why not use this instead: &lt;a rel="nofollow" target="_new" href="http://support.microsoft.com/kb/815813"&gt;support.microsoft.com/.../815813&lt;/a&gt; ? It&amp;#39;s native .NET&lt;/p&gt;
&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7139169" width="1" height="1"&gt;</description></item><item><title>re: Serializing and Deserializing Objects…to and from…XML</title><link>http://weblogs.asp.net/stevewellens/archive/2009/07/02/serializing-and-deserializing-objects-to-and-from-xml.aspx#7138846</link><pubDate>Thu, 02 Jul 2009 20:07:03 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7138846</guid><dc:creator>SGWellens</dc:creator><author>SGWellens</author><description>&lt;p&gt;If a field is added to the class, the reconstitution of an object from an old XML string will ignore the new field. &amp;nbsp;However, you can create a default constructor that initializes new fields to their desired defaults. &amp;nbsp;&lt;/p&gt;
&lt;p&gt;This is much better than doing a binary serialization where you would need to write a conversion program each time a new version was issued. &amp;nbsp;&lt;/p&gt;
&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7138846" width="1" height="1"&gt;</description></item><item><title>re: Serializing and Deserializing Objects…to and from…XML</title><link>http://weblogs.asp.net/stevewellens/archive/2009/07/02/serializing-and-deserializing-objects-to-and-from-xml.aspx#7138804</link><pubDate>Thu, 02 Jul 2009 18:34:13 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7138804</guid><dc:creator>Dave</dc:creator><author>Dave</author><description>&lt;p&gt;What if a field is added to the class after the xml file is created. Will it throw an error if its missing in the xml when you try to reconstitute it?&lt;/p&gt;
&lt;p&gt;If it does throw the error will it say what field is missing?&lt;/p&gt;
&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7138804" width="1" height="1"&gt;</description></item><item><title>re: Serializing and Deserializing Objects…to and from…XML</title><link>http://weblogs.asp.net/stevewellens/archive/2009/07/02/serializing-and-deserializing-objects-to-and-from-xml.aspx#7138759</link><pubDate>Thu, 02 Jul 2009 16:57:14 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7138759</guid><dc:creator>Scott Allen</dc:creator><author>Scott Allen</author><description>&lt;p&gt;I think a generic parameter on deserialize would be nice, because the caller would not have to cast the return value, and you&amp;#39;d have a cleaner method signature. The type coercion would be hidden inside deserialize. &lt;/p&gt;
&lt;p&gt;public static T DeSerialize&amp;lt;T&amp;gt;(string XmlOfAnObject) &lt;/p&gt;
&lt;p&gt;{ &lt;/p&gt;
&lt;p&gt;&amp;nbsp; &amp;nbsp;... &lt;/p&gt;
&lt;p&gt;&amp;nbsp; &amp;nbsp;return (T)anObject;&lt;/p&gt;
&lt;p&gt;&amp;nbsp; &amp;nbsp;...&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;Gives you:&lt;/p&gt;
&lt;p&gt;var foo = Deserialize&amp;lt;Bar&amp;gt;(xml);&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7138759" width="1" height="1"&gt;</description></item></channel></rss>