<?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>Nephandus &#187; documentation</title>
	<atom:link href="http://www.nephandus.com/tag/documentation/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.nephandus.com</link>
	<description>Just another WordPress weblog</description>
	<lastBuildDate>Tue, 03 Jan 2012 16:33:48 +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>Wazza ListItemContainer?</title>
		<link>http://www.nephandus.com/2010/06/30/wazza-listitemcontainer/</link>
		<comments>http://www.nephandus.com/2010/06/30/wazza-listitemcontainer/#comments</comments>
		<pubDate>Wed, 30 Jun 2010 13:57:42 +0000</pubDate>
		<dc:creator>Chris</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[documentation]]></category>
		<category><![CDATA[quirks]]></category>
		<category><![CDATA[serialization]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://www.nephandus.com/?p=24</guid>
		<description><![CDATA[VirPack has me working on an application that needs a user administered list of options for people to choose from. It&#8217;s a fairly simple thing that doesn&#8217;t require relational tables so I thought I&#8217;d toss it into XML. Now, C# deals with XML really well. Almost everything, if you just ask nicely, will be quickly [...]]]></description>
			<content:encoded><![CDATA[<div class="wp-caption alignleft" style="width: 250px"><a href="http://www.flickr.com/photos/scottpargettphoto/1838997549/"><img title="Springfield XD .40 XML" src="http://farm3.static.flickr.com/2172/1838997549_30246e88ef_m.jpg" alt="" width="240" height="160" /></a><p class="wp-caption-text">Not this kind of XML</p></div>
<p>VirPack has me working on an application that needs a user administered list of options for people to choose from.  It&#8217;s a fairly simple thing that doesn&#8217;t require relational tables so I thought I&#8217;d toss it into XML.</p>
<p>Now, C# deals with XML really well.  Almost everything, if you just ask nicely, will be quickly and easily packed off into XML the framework with just a few lines of code.</p>
<p><span id="more-24"></span></p>
<pre class="brush: csharp; title: ;">
String path = Server.MapPath(&quot;~\\myfile.xml&quot;);
using (FileStream sourceFile = new FileStream(Server.MapPath(path), FileMode.Create, FileAccess.Write))
        {
            XmlSerializer serializer = new XmlSerializer(typeof(Foo));
            serializer.Serialize(sourceFile, bar);
        }
        bar = null;
        using (System.IO.FileStream sourceFile = new System.IO.FileStream(path, System.IO.FileMode.Open, System.IO.FileAccess.Read))
        {
            System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(Foo));
            Foo bar= serializer.Deserialize(sourceFile) as Foo;
        }
</pre>
<p>As simple as that I&#8217;ve packed my class off to be serialized and then unserialized it.  Three lines of code is hard to beat for XML serialization and deserialization.  &#8220;Foo&#8221; is a user defined type, however, and my particular task being the population of a drop-down list, no such complexity was required.  Yet, when I tried to feed a List of ListItems into the Serializer everything broke.</p>
<pre class="brush: csharp; title: ;">
//This breaks
System.Xml.Serialization.XmlSerializer listSerializer = new System.Xml.Serialization.XmlSerializer(typeof(List));
//This too
System.Xml.Serialization.XmlSerializer typedListSerializer = new System.Xml.Serialization.XmlSerializer(typeof(List&lt;ListItem&gt;));
</pre>
<p>This has to do with how C# deals with collections.  Serialization requires a root level node and a collection, despite having an appearance of such, has no root level information.  It is a collection, not a collection bounded by something else and XML serialization requires that something else.</p>
<p>So C# has a hackish workaround.  The ListItemContainer class is a simple type that contains a List of ListItems.</p>
<pre class="brush: csharp; title: ;">
//This works
System.Xml.Serialization.XmlSerializer listSerializer = new System.Xml.Serialization.XmlSerializer(typeof(ListItemContainer));
</pre>
<p>Of course, not everything you&#8217;ll wish to serialize in a list will be a list item, but the fact that C# had to create it&#8217;s own little wrapper class should be a clue for developers.  Getting a collection into XML requires a wrapper class; it&#8217;s as simple as that.</p>
<p>Would it have killed them to document that though?</p>
]]></content:encoded>
			<wfw:commentRss>http://www.nephandus.com/2010/06/30/wazza-listitemcontainer/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

