<?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>Alteridem Consulting &#187; Generics</title>
	<atom:link href="http://www.alteridem.net/category/net/generics/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.alteridem.net</link>
	<description>Software by Design</description>
	<lastBuildDate>Fri, 02 Sep 2011 14:21:27 +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>Introduction to XML Serialization</title>
		<link>http://www.alteridem.net/2007/08/13/introduction-to-xml-serialization/</link>
		<comments>http://www.alteridem.net/2007/08/13/introduction-to-xml-serialization/#comments</comments>
		<pubDate>Mon, 13 Aug 2007 13:45:43 +0000</pubDate>
		<dc:creator>Robert Prouse</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Generics]]></category>
		<category><![CDATA[XML]]></category>

		<guid isPermaLink="false">http://www.alteridem.net/2007/08/13/introduction-to-xml-serialization/</guid>
		<description><![CDATA[Many projects that I work on require me to serialize objects out to the file system and then retrieve them later. When I am serializing out program settings that are loaded in every time the program runs, I tend to use the Settings that come as a part of C# projects. If however the user [...]]]></description>
			<content:encoded><![CDATA[<p>Many projects that I work on require me to serialize objects out to the file system and then retrieve them later. When I am serializing out program settings that are loaded in every time the program runs, I tend to use the Settings that come as a part of C# projects. If however the user can load or save settings, documents or projects, then I use the <strong>XmlSerializer</strong> class to save instances of a class out to XML. This allows me to automatically create a new document type based on my data classes and save them out with a few lines of code.</p>
<p>Because I do this so often, I have created a static helper class that has two methods that serialize/deserialize any type to/from an XML file. The two methods are:</p>
<pre class="hl"><span class="hl kwa">public static</span> <span class="hl kwb">void</span> Serialize<span class="hl sym">&lt;</span>T<span class="hl sym">&gt;(</span> <span class="hl kwb">string</span> filename<span class="hl sym">,</span> T data <span class="hl sym">)</span>
<span class="hl sym">{</span>
   TextWriter writer <span class="hl sym">=</span> <span class="hl kwa">null</span><span class="hl sym">;</span>
   <span class="hl kwa">try</span>
   <span class="hl sym">{</span>
      writer <span class="hl sym">=</span> <span class="hl kwa">new</span> <span class="hl kwd">StreamWriter</span><span class="hl sym">(</span> filename <span class="hl sym">);</span>
      XmlSerializer serializer <span class="hl sym">=</span> <span class="hl kwa">new</span> <span class="hl kwd">XmlSerializer</span><span class="hl sym">(</span> <span class="hl kwd">typeof</span><span class="hl sym">(</span>T<span class="hl sym">) );</span>
      serializer<span class="hl sym">.</span><span class="hl kwd">Serialize</span><span class="hl sym">(</span> writer<span class="hl sym">,</span> data <span class="hl sym">);</span>
   <span class="hl sym">}</span>
   <span class="hl kwa">finally</span>
   <span class="hl sym">{</span>
      <span class="hl kwa">if</span> <span class="hl sym">(</span> writer <span class="hl sym">!=</span> <span class="hl kwa">null</span> <span class="hl sym">)</span>
      <span class="hl sym">{</span>
         writer<span class="hl sym">.</span><span class="hl kwd">Close</span><span class="hl sym">();</span>
      <span class="hl sym">}</span>
   <span class="hl sym">}</span>
<span class="hl sym">}</span>

<span class="hl kwa">public static</span> T Deserialize<span class="hl sym">&lt;</span>T<span class="hl sym">&gt;(</span> <span class="hl kwb">string</span> filename <span class="hl sym">)</span>
<span class="hl sym">{</span>
   TextReader reader <span class="hl sym">=</span> <span class="hl kwa">null</span><span class="hl sym">;</span>
   T data <span class="hl sym">=</span> <span class="hl kwa">default</span><span class="hl sym">(</span>T<span class="hl sym">);</span>
   <span class="hl kwa">try</span>
   <span class="hl sym">{</span>
      reader <span class="hl sym">=</span> <span class="hl kwa">new</span> <span class="hl kwd">StreamReader</span><span class="hl sym">(</span> filename <span class="hl sym">);</span>
      XmlSerializer serializer <span class="hl sym">=</span> <span class="hl kwa">new</span> <span class="hl kwd">XmlSerializer</span><span class="hl sym">(</span> <span class="hl kwd">typeof</span><span class="hl sym">(</span>T<span class="hl sym">) );</span>
      data <span class="hl sym">= (</span> T <span class="hl sym">)</span>serializer<span class="hl sym">.</span><span class="hl kwd">Deserialize</span><span class="hl sym">(</span> reader <span class="hl sym">);</span>
   <span class="hl sym">}</span>
   <span class="hl kwa">finally</span>
   <span class="hl sym">{</span>
      <span class="hl kwa">if</span> <span class="hl sym">(</span> reader <span class="hl sym">!=</span> <span class="hl kwa">null</span> <span class="hl sym">)</span>
      <span class="hl sym">{</span>
         reader<span class="hl sym">.</span><span class="hl kwd">Close</span><span class="hl sym">();</span>
      <span class="hl sym">}</span>
   <span class="hl sym">}</span>
   <span class="hl kwa">return</span> data<span class="hl sym">;</span>
<span class="hl sym">}</span></pre>
<p></p>
<p>With these two methods, you can serialize a class out to XML or restore a class from XML with one line of code.</p>
<pre class="hl"><span class="hl slc">// Serialize out a person to XML</span>
XmlSerializerHelper<span class="hl sym">.</span>Serialize<span class="hl sym">&lt;</span>Person<span class="hl sym">&gt;(</span> <span class="hl str">"person.xml"</span><span class="hl sym">,</span> person <span class="hl sym">);</span>

<span class="hl slc">// Deserialize the person from XML</span>
Person person <span class="hl sym">=</span> XmlSerializerHelper<span class="hl sym">.</span>Deserialize<span class="hl sym">&lt;</span>Person<span class="hl sym">&gt;(</span> <span class="hl str">"person.xml"</span> <span class="hl sym">);</span></pre>
<p></p>
<p>The XmlSerializer class will serialize out/in all public properties and members of the given class. One common error is that the class that you are serializing and all contained classes must have a public constructor that takes no parameters.</p>
<p>The class that you are serializing does not need to be modified, but if you want to control how the XML is serialized out, you can use several attributes.</p>
<p>By default, all properties are serialized out as XML elements. If you want to specify how the elements are serialized out, you can use the <strong>XmlElementAttribute</strong> and set properties on it such as <strong>ElementName</strong>.</p>
<p>Simple types such as string and int can be serialized out as Xml attributes if you prefer. To do this, mark the properties with the <strong>XmlAttributeAttribute</strong>.</p>
<p>If you want properties in your class to not be serialized, use the <strong>XmlIgnoreAttribute</strong>. This will usually be used for properties that are calculated based on the values of other properties.</p>
<p>For more, look at this <a href="http://www.alteridem.net/wp-content/uploads/2007/08/xmlserialization.zip" title="Xml Serialization Example Project">Xml Serialization example project</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.alteridem.net/2007/08/13/introduction-to-xml-serialization/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>

