<?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; serialization</title>
	<atom:link href="http://www.nephandus.com/tag/serialization/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>
		<item>
		<title>C# WebControl Behavior</title>
		<link>http://www.nephandus.com/2010/04/26/c-webcontrol-behavior/</link>
		<comments>http://www.nephandus.com/2010/04/26/c-webcontrol-behavior/#comments</comments>
		<pubDate>Mon, 26 Apr 2010 18:52:36 +0000</pubDate>
		<dc:creator>Chris</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[serialization]]></category>
		<category><![CDATA[serialized]]></category>
		<category><![CDATA[session]]></category>
		<category><![CDATA[viewstate]]></category>
		<category><![CDATA[webcontrol]]></category>

		<guid isPermaLink="false">http://www.nephandus.com/?p=14</guid>
		<description><![CDATA[My &#8220;learn C# project&#8221; at work has centered around creating a drag-and-drop portlet style system for the display of custom widgets.  I&#8217;ve been using JQuery UI for the javascript functionality but the backend has been all custom C# work. People familiar with C# know that C# supports the inclusion of user defined controls called WebControls.  [...]]]></description>
			<content:encoded><![CDATA[<p>My &#8220;learn C# project&#8221; at work has centered around creating a drag-and-drop portlet style system for the display of custom widgets.  I&#8217;ve been using JQuery UI for the javascript functionality but the backend has been all custom C# work.</p>
<p>People familiar with C# know that C# supports the inclusion of user defined controls called WebControls.  These are more or less very simple C# programs which can be man-handled by another bit of C# code.  They&#8217;re handy for making your code modular: you might design a web-control that takes and validates a credit card number, for example.</p>
<p>But WebControls are notoriously tricky beasts and over the course of the last few weeks I&#8217;ve come to understand that one of the reasons for this is that they don&#8217;t behave quite the way you might expect them to when they are serialized and deserialized.<span id="more-14"></span></p>
<p>Most C# serialization occurs behind the scenes in something called the ViewState.  For those of you who have no idea what that means, ViewState is C#&#8217;s rather clever way of persisting the things on a webpage so that it feels a little more like a desktop application.  The long and the short of its behavior is that things get serialized into the ViewState just before rendering and de-serialized back out of ViewState on the next request as part of the load step.</p>
<p>Most controls go into the ViewState&#8230; but WebControls don&#8217;t.  At least, they don&#8217;t go in there by default.  Presumably you can create custom ViewState serialization for them; I have not tried this.  Instead, I serialized my WebControls very simply into Session and coded that storage into a property.  My code looked something like this</p>
<pre class="brush: csharp; title: ;">
private Widget InternalWidget
{
	get
	{
		Widget result;
		if (Session[&quot;Widget&quot; + this.UniqueID] != null)
		{
			result = Session[&quot;Widget&quot; + this.UniqueID] as Widget;
			if (result == null )
			{
				result = WidgetFactory.Create();
				result.Arguments = InternalActivePortlet.Arguments;
			}
		}
		return result;
	}
	set
	{
		Session[&quot;Widget&quot; + this.UniqueID] = value;
	}
}
</pre>
<p>Now, as expected, none of my EventHandlers persisted when the WebControl came back out of its session serialization.  The object that comes back from serialization is an entirely new object; the old one has been destroyed.  As a result, the event handlers in the deserialized object point to null function pointers.  C# doesn&#8217;t throw an error when you call a null function pointer, however; it just goes on its merry way and does nothing.</p>
<p>But the function pointers &#8211; even though they now point to null objects &#8211; remain.  C# remembers a great deal more about the session-serialized object than one might think including &#8211; and this is the part that really threw me &#8211; the fact that it&#8217;s already been through the ASP parser.</p>
<p>I had expected, since my event handlers weren&#8217;t working when the WebControl came back from Session, that the events themselves were gone and that therefore the object returned from session was new &#8212; as if it was programatically created for the first time.  This is not the case.  When you programatically create a WebControl for the first time it&#8217;s ASP file is parsed and event handlers are set up based upon the code there.  When you deserialize a WebControl from session, however, C# remembers that it has already dealt with its ASP code and does not do it a second time.</p>
<p>Consequently, none of the event handlers defined in the ASP work either.</p>
<p>I expect that all of this seems rudimentary to more experienced C# developers, but to me it was quite a mystery and it took me some time to work out the above logic.  Hopefully this will aid some neophyte coder working through the same issues I had.</p>
<p>On a totally unrelated note: I need to find a decent code display plugin for WordPress&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.nephandus.com/2010/04/26/c-webcontrol-behavior/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

