<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: XmlSerializer Misconceptions Part 1</title>
	<atom:link href="http://blog.nickgravelyn.com/2009/04/xmlserializer-misconceptions-part-1/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.nickgravelyn.com/2009/04/xmlserializer-misconceptions-part-1/</link>
	<description></description>
	<lastBuildDate>Fri, 23 Jul 2010 01:34:59 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=abc</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: paulecoyote.com/</title>
		<link>http://blog.nickgravelyn.com/2009/04/xmlserializer-misconceptions-part-1/comment-page-1/#comment-160</link>
		<dc:creator>paulecoyote.com/</dc:creator>
		<pubDate>Tue, 28 Apr 2009 16:28:39 +0000</pubDate>
		<guid isPermaLink="false">http://blog.nickgravelyn.com/?p=671#comment-160</guid>
		<description>You know something I&#039;ve yet to find a satisifactory answer too, is that I would expect XmlSerializer to be isomorphic but it does not appear to be.  Basically the [DefaultValue(true)] is used during serialisation but not deserialastion.  Of course that means something just written to a stream and read back in to another object has different values.

namespace Noddy2

{

      #region Class to be serialised

      [Serializable()]

      public class Clue

      {

            [DefaultValue(&quot;Nothing To See Here&quot;)]     //Comment out and it won&#039;t assert

            public string seen;//Set &quot;Nothing To See Here&quot; here to to work around

      };

      #endregion



      class Program

      {

            static void Main(string[] args)

            {

                  Clue clue = new Clue();

                  clue.seen = &quot;Nothing To See Here&quot;;





                  //Serialise the clue...

                  StringBuilder sb = new StringBuilder();

                  XmlSerializer xs = new XmlSerializer(typeof(Clue));

                  using (StringWriter sw = new StringWriter(sb))

                  {

                        xs.Serialize(sw, clue);

                  }

                  string serialised = sb.ToString();



                  //Show serialised version of the clue...

                  Console.WriteLine(&quot;Clue1:&quot;);

                  Console.WriteLine(serialised);



                  //Deserialise the clue...

                  Clue clue2;

                  using (StringReader sr = new StringReader(serialised))

                  {

                        clue2 = (Clue)xs.Deserialize(sr);

                  }



                  Debug.Assert(clue.seen == clue2.seen, &quot;Clues don&#039;t match&quot;);





                  //Show clue 2 on console

                  sb.Length = 0;

                  using (StringWriter sw = new StringWriter(sb))

                  {

                        xs.Serialize(sw, clue2);

                        Console.WriteLine(&quot;Clue2:&quot;);

                        Console.WriteLine(sb.ToString());

                  }

            }

      }

}</description>
		<content:encoded><![CDATA[<p>You know something I&#8217;ve yet to find a satisifactory answer too, is that I would expect XmlSerializer to be isomorphic but it does not appear to be.  Basically the [DefaultValue(true)] is used during serialisation but not deserialastion.  Of course that means something just written to a stream and read back in to another object has different values.</p>
<p>namespace Noddy2</p>
<p>{</p>
<p>      #region Class to be serialised</p>
<p>      [Serializable()]</p>
<p>      public class Clue</p>
<p>      {</p>
<p>            [DefaultValue("Nothing To See Here")]     //Comment out and it won&#8217;t assert</p>
<p>            public string seen;//Set &#8220;Nothing To See Here&#8221; here to to work around</p>
<p>      };</p>
<p>      #endregion</p>
<p>      class Program</p>
<p>      {</p>
<p>            static void Main(string[] args)</p>
<p>            {</p>
<p>                  Clue clue = new Clue();</p>
<p>                  clue.seen = &#8220;Nothing To See Here&#8221;;</p>
<p>                  //Serialise the clue&#8230;</p>
<p>                  StringBuilder sb = new StringBuilder();</p>
<p>                  XmlSerializer xs = new XmlSerializer(typeof(Clue));</p>
<p>                  using (StringWriter sw = new StringWriter(sb))</p>
<p>                  {</p>
<p>                        xs.Serialize(sw, clue);</p>
<p>                  }</p>
<p>                  string serialised = sb.ToString();</p>
<p>                  //Show serialised version of the clue&#8230;</p>
<p>                  Console.WriteLine(&#8220;Clue1:&#8221;);</p>
<p>                  Console.WriteLine(serialised);</p>
<p>                  //Deserialise the clue&#8230;</p>
<p>                  Clue clue2;</p>
<p>                  using (StringReader sr = new StringReader(serialised))</p>
<p>                  {</p>
<p>                        clue2 = (Clue)xs.Deserialize(sr);</p>
<p>                  }</p>
<p>                  Debug.Assert(clue.seen == clue2.seen, &#8220;Clues don&#8217;t match&#8221;);</p>
<p>                  //Show clue 2 on console</p>
<p>                  sb.Length = 0;</p>
<p>                  using (StringWriter sw = new StringWriter(sb))</p>
<p>                  {</p>
<p>                        xs.Serialize(sw, clue2);</p>
<p>                        Console.WriteLine(&#8220;Clue2:&#8221;);</p>
<p>                        Console.WriteLine(sb.ToString());</p>
<p>                  }</p>
<p>            }</p>
<p>      }</p>
<p>}</p>
]]></content:encoded>
	</item>
</channel>
</rss>
