<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Dave Amenta .com &#187; XML</title>
	<atom:link href="http://www.daveamenta.com/tag/xml/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.daveamenta.com</link>
	<description>(dot (at dave daveamenta) com)</description>
	<lastBuildDate>Tue, 31 Jan 2012 17:22:23 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>HowTo: Save and retrieve C# objects in XML</title>
		<link>http://www.daveamenta.com/2008-05/howto-save-and-retrieve-c-objects-in-xml/</link>
		<comments>http://www.daveamenta.com/2008-05/howto-save-and-retrieve-c-objects-in-xml/#comments</comments>
		<pubDate>Sun, 04 May 2008 19:00:44 +0000</pubDate>
		<dc:creator>Dave</dc:creator>
				<category><![CDATA[Code Snippets]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[XML]]></category>
		<category><![CDATA[XMLSerialization]]></category>

		<guid isPermaLink="false">http://www.daveamenta.com/?p=18</guid>
		<description><![CDATA[I&#8217;ve spent a bit of time tweaking this XML Serialization class I wrote to make use of the System.Xml.Serialization objects Microsoft provides in .NET 2.0. XML Serialization is a great way to store complex data types, and also a great alternative to binary serialization. Unfortunately, using these classes is not as straightforward as I would [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: center;"><a href="http://www.daveamenta.com/wp-content/uploads/2008/05/net_logo.jpg"><img class="alignnone size-full wp-image-19 aligncenter" title="net_logo" src="http://www.daveamenta.com/wp-content/uploads/2008/05/net_logo.jpg" alt="" width="261" height="168" /></a></p>
<p>I&#8217;ve spent a bit of time tweaking <a href="/download/snippets/XML.XSerial.cs" target="_blank">this XML Serialization class</a> I wrote to make use of the System.Xml.Serialization objects Microsoft provides in .NET 2.0.</p>
<p>XML Serialization is a great way to store complex data types, and also a great alternative to binary serialization.  Unfortunately, using these classes is not as straightforward as I would like, there isn&#8217;t any simple Save or Load methods, I&#8217;ve bridged that gap with a a small class that provides this functionality.</p>
<p><strong>Basic Methods:</strong></p>
<p>These are the two basic methods you would need to save and load an object from a file.</p>
<ul>
<li>public static T Load&lt;T&gt;(string filename)</li>
<li>public static void Save&lt;T&gt;(string filename, T cls)</li>
</ul>
<p><strong>Additional Methods:</strong></p>
<p>These methods are included for those that would like to bypass the files, and send objects directly over a different medium.</p>
<ul>
<li>public static String SerializeObject&lt;T&gt;(T pObject)</li>
<li>public static T DeserializeObject&lt;T&gt;(String pXmlizedString)</li>
</ul>
<p><strong>Important Note:</strong></p>
<p>Remember to wrap all of these methods in try-catch blocks and catch any exceptions thrown. If the file cannot be accessed, or there is an error during serialization, it will be passed on.</p>
<p><strong>Code Example:</strong></p>
<p>Load an object from file:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">List MyList <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">try</span>
<span style="color: #008000;">&#123;</span>
MyList <span style="color: #008000;">=</span> XSerial<span style="color: #008000;">.</span><span style="color: #0000FF;">Load</span><span style="color: #008000;">&lt;</span>list<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;MyList.xml&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span>
<span style="color: #0600FF; font-weight: bold;">catch</span> <span style="color: #008000;">&#40;</span>Exception e<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Error: &quot;</span> <span style="color: #008000;">+</span> e<span style="color: #008000;">.</span><span style="color: #0000FF;">Message</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008080; font-style: italic;">// make sure the object won't cause errors.</span>
MyList <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> List<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
Save an <span style="color: #6666cc; font-weight: bold;">object</span> to file<span style="color: #008000;">:</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">try</span>
<span style="color: #008000;">&#123;</span>
XSerial<span style="color: #008000;">.</span><span style="color: #0000FF;">Save</span><span style="color: #008000;">&lt;/</span>list<span style="color: #008000;">&gt;&lt;</span>list<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;MyList.xml&quot;</span>, MyList<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span>
<span style="color: #0600FF; font-weight: bold;">catch</span> <span style="color: #008000;">&#40;</span>Exception e<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Error Saving: &quot;</span> <span style="color: #008000;">+</span> e<span style="color: #008000;">.</span><span style="color: #0000FF;">Message</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008080; font-style: italic;">// nothing can be done about recovery.</span>
<span style="color: #008000;">&#125;</span><span style="color: #008000;">&lt;/</span>list<span style="color: #008000;">&gt;</span></pre></div></div>

<p><strong>Download Demo Project:</strong></p>
<p><a href="/download/XMLSerialTest.zip">XMLSerialTest</a> [ZIP] [Visual Studio 2008]</p>
]]></content:encoded>
			<wfw:commentRss>http://www.daveamenta.com/2008-05/howto-save-and-retrieve-c-objects-in-xml/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

