<?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>Hans Rasmussen &#187; xml</title>
	<atom:link href="http://www.hansrasmussen.com/tag/xml/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.hansrasmussen.com</link>
	<description>info@hansrasmussen.com, +46 (0)723 207008</description>
	<lastBuildDate>Wed, 01 Feb 2012 11:59:50 +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>DataContractSerializer &#8211; Alphabetical mystique</title>
		<link>http://www.hansrasmussen.com/2010/02/datacontractserializer-alphabetical-mystique/</link>
		<comments>http://www.hansrasmussen.com/2010/02/datacontractserializer-alphabetical-mystique/#comments</comments>
		<pubDate>Fri, 19 Feb 2010 23:46:47 +0000</pubDate>
		<dc:creator>Hans Rasmussen</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[serialization]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://www.hansrasmussen.com/2010/02/datacontractserializer-alphabetical-mystique/</guid>
		<description><![CDATA[http://msdn.microsoft.com/en-us/library/system.runtime.serialization.datacontractserializer.aspx I noticed that DataContractSerializer is using serialization in alphabetical order. If you do not follow the below rules you will end up missing data after deserialization of your XML.     [Serializable, DataContract(Namespace = "your-namespace")]     public class YourObject : DataModelDeserializable&#60;YourObject&#62;     {         [DataMember]         public string PostalCode { get; set; }         [DataMember] [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://msdn.microsoft.com/en-us/library/system.runtime.serialization.datacontractserializer.aspx">http://msdn.microsoft.com/en-us/library/system.runtime.serialization.datacontractserializer.aspx</a></p>
<p>I noticed that DataContractSerializer is using serialization in alphabetical order. If you do not follow the below rules you will end up missing data after deserialization of your XML.</p>
<p>    [Serializable, DataContract(Namespace = "your-namespace")]</p>
<p>    public class YourObject : DataModelDeserializable&lt;YourObject&gt;</p>
<p>    {</p>
<p>        [DataMember]</p>
<p>        public string PostalCode { get; set; }</p>
<p>        [DataMember]</p>
<p>        public string City { get; set; }</p>
<p>        [DataMember]</p>
<p>        public DateTime Street { get; set; }</p>
<p>    }</p>
<p>If you would serialize the above the XML would be in <strong>alphabetical</strong> order;</p>
<p>&lt;City&gt;New York&lt;/City&gt;</p>
<p>&lt;PostalCode&gt;23001&lt;/PostalCode&gt;</p>
<p>&lt;Street&gt;1st Avenue&lt;/Street&gt;</p>
<p>If you would now create your XML from another component, intending to use it with the deserialize process you might mistake the alphabetical order;</p>
<p>&lt;Street&gt;1st Avenue&lt;/Street&gt;</p>
<p>&lt;PostalCode&gt;23001&lt;/PostalCode&gt;</p>
<p>&lt;City&gt;New York&lt;/City&gt;</p>
<p>You will not notice that the properties are not filled with any values from the elements in the XML. The reason for this is the alphabetical processing. You might even discover that only some properties are correct because you start with having some in the right alphabetical order but fail with the rest of the XML and the equivalent properties.</p>
<p>You can solve this in 2 ways;</p>
<p>1. Make sure that the XML file elements are in alphabetical order.</p>
<p>2. The first might fail because you are limited to control the element order so the you can instruct DataContractSerializer that it should order its serialization differently.</p>
<p>    [Serializable, DataContract(Namespace = "your-namespace")]</p>
<p>    public class YourObject : DataModelDeserializable&lt;YourObject&gt;</p>
<p>    {</p>
<p>        [DataMember(Order=2)]</p>
<p>        public string PostalCode { get; set; }</p>
<p>        [DataMember(Order=3)]</p>
<p>        public string City { get; set; }</p>
<p>        [DataMember(Order=1)]</p>
<p>        public DateTime Street { get; set; }</p>
<p>    }</p>
<p>/* now the serialization process is done in order Street, PostalCode, City */</p>
]]></content:encoded>
			<wfw:commentRss>http://www.hansrasmussen.com/2010/02/datacontractserializer-alphabetical-mystique/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

