<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0">
  <channel>
    <title>IIrrelevant - XmlSerializer</title>
    <link>http://www.milkcarton.com/blog/</link>
    <description>Irrelevant musings about software development</description>
    <language>en-us</language>
    <copyright>Dan Morphis</copyright>
    <lastBuildDate>Thu, 13 Sep 2007 15:22:29 GMT</lastBuildDate>
    <generator>newtelligence dasBlog 2.3.9074.18820</generator>
    <managingEditor>blog@milkcarton.com</managingEditor>
    <webMaster>blog@milkcarton.com</webMaster>
    <item>
      <trackback:ping>http://www.milkcarton.com/blog/Trackback.aspx?guid=9fe3849a-f364-43ea-acb4-b100e991e1cc</trackback:ping>
      <pingback:server>http://www.milkcarton.com/blog/pingback.aspx</pingback:server>
      <pingback:target>http://www.milkcarton.com/blog/PermaLink,guid,9fe3849a-f364-43ea-acb4-b100e991e1cc.aspx</pingback:target>
      <dc:creator>Dan Morphis</dc:creator>
      <wfw:comment>http://www.milkcarton.com/blog/CommentView,guid,9fe3849a-f364-43ea-acb4-b100e991e1cc.aspx</wfw:comment>
      <wfw:commentRss>http://www.milkcarton.com/blog/SyndicationService.asmx/GetEntryCommentsRss?guid=9fe3849a-f364-43ea-acb4-b100e991e1cc</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Usually I post solutions I've discovered or created to problems I've encountered. 
Now, I've run into a problem with the XmlSerializer and I need your help solving it.  Scroll
to the bottom for an update.
</p>
        <h3>The Problem
</h3>
        <p>
I have a class called Foobar.  Foobar inherits from List&lt;string&gt;, and contains
one member Title.  When I use the XmlSerializer to serialize and deserialize
Foobar, all the items in the List are make it through safely, but the member Title
does not.
</p>
        <h3>The Code
</h3>
        <p>
        </p>
        <div class="wlWriterSmartContent" id="57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:cd057b70-45a7-4b72-a7db-655bf0456e1b" contenteditable="false" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">
          <pre style="background-color:#FFFF80;">
            <div>
              <!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->
              <span style="color: #0000FF; ">using</span>
              <span style="color: #000000; "> System; </span>
              <span style="color: #0000FF; ">using</span>
              <span style="color: #000000; "> System.Collections.Generic; </span>
              <span style="color: #0000FF; ">using</span>
              <span style="color: #000000; "> System.IO; </span>
              <span style="color: #0000FF; ">using</span>
              <span style="color: #000000; "> System.Xml.Serialization; </span>
              <span style="color: #0000FF; ">internal</span>
              <span style="color: #000000; ">
              </span>
              <span style="color: #0000FF; ">class</span>
              <span style="color: #000000; "> Program
{ </span>
              <span style="color: #0000FF; ">private</span>
              <span style="color: #000000; ">
              </span>
              <span style="color: #0000FF; ">static</span>
              <span style="color: #000000; ">
              </span>
              <span style="color: #0000FF; ">void</span>
              <span style="color: #000000; "> Main()
{ Foobar f </span>
              <span style="color: #000000; ">=</span>
              <span style="color: #000000; ">
              </span>
              <span style="color: #0000FF; ">new</span>
              <span style="color: #000000; "> Foobar();
f.Add(</span>
              <span style="color: #000000; ">"</span>
              <span style="color: #000000; ">1</span>
              <span style="color: #000000; ">"</span>
              <span style="color: #000000; ">);
f.Add(</span>
              <span style="color: #000000; ">"</span>
              <span style="color: #000000; ">2</span>
              <span style="color: #000000; ">"</span>
              <span style="color: #000000; ">);
f.Title </span>
              <span style="color: #000000; ">=</span>
              <span style="color: #000000; ">
              </span>
              <span style="color: #000000; ">"</span>
              <span style="color: #000000; ">Title</span>
              <span style="color: #000000; ">"</span>
              <span style="color: #000000; ">;
MemoryStream mem </span>
              <span style="color: #000000; ">=</span>
              <span style="color: #000000; ">
              </span>
              <span style="color: #0000FF; ">new</span>
              <span style="color: #000000; "> MemoryStream();
XmlSerializer xs </span>
              <span style="color: #000000; ">=</span>
              <span style="color: #000000; ">
              </span>
              <span style="color: #0000FF; ">new</span>
              <span style="color: #000000; "> XmlSerializer(</span>
              <span style="color: #0000FF; ">typeof</span>
              <span style="color: #000000; ">(Foobar));
xs.Serialize(mem, f); mem.Seek(</span>
              <span style="color: #000000; ">0</span>
              <span style="color: #000000; ">, </span>
              <span style="color: #000000; ">0</span>
              <span style="color: #000000; ">);
mem.Seek(</span>
              <span style="color: #000000; ">0</span>
              <span style="color: #000000; ">, </span>
              <span style="color: #000000; ">0</span>
              <span style="color: #000000; ">);
Foobar deser </span>
              <span style="color: #000000; ">=</span>
              <span style="color: #000000; "> (Foobar)xs.Deserialize(mem);
Console.WriteLine(deser); } } [Serializable] </span>
              <span style="color: #0000FF; ">public</span>
              <span style="color: #000000; ">
              </span>
              <span style="color: #0000FF; ">class</span>
              <span style="color: #000000; "> Foobar
: List</span>
              <span style="color: #000000; ">&lt;</span>
              <span style="color: #0000FF; ">string</span>
              <span style="color: #000000; ">&gt;</span>
              <span style="color: #000000; "> { </span>
              <span style="color: #0000FF; ">public</span>
              <span style="color: #000000; ">
              </span>
              <span style="color: #0000FF; ">string</span>
              <span style="color: #000000; "> Title;
}</span>
            </div>
          </pre>
        </div>
        <h3>The Solution
</h3>
        <p>
One way I came up with to solve this problem is make Foobar not inherit from List&lt;string&gt;,
and make a member called Items that is a List&lt;string&gt;, but that's kind of hokey. 
I've searched Google and can't find a solution.  So I'm appealing to my dear
readers for a better solution.
</p>
        <h3>Update
</h3>
        <p>
Looks like I was being way to specific in my Google search, searching on "XmlSerializer
List" gives us this <a title="http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=566175&amp;SiteID=1" href="http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=566175&amp;SiteID=1">http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=566175&amp;SiteID=1</a> as
the first item.  In the post, 
</p>
        <p>
Elena Kharitidi says "XmlSerializer does not serialize any members if a collection.
Only collection items get serialized. This is by design, basically a decision was
made to handle collections as arrays not as classes with multiple properties, so collections
should look like arrays on the wire, therefore they do not have any members other
then collection items, and can be “flattened” by adding the [XmlElement] to the member
of the ICollection type." 
</p>
        <p>
Long and short, I need to use the hokey solution I came up with (or something like
it), or write my own serializer.
</p>
        <img width="0" height="0" src="http://www.milkcarton.com/blog/aggbug.ashx?id=9fe3849a-f364-43ea-acb4-b100e991e1cc" />
      </body>
      <title>Problems serializing class which inherits from a generic list</title>
      <guid isPermaLink="false">http://www.milkcarton.com/blog/PermaLink,guid,9fe3849a-f364-43ea-acb4-b100e991e1cc.aspx</guid>
      <link>http://www.milkcarton.com/blog/2007/09/13/Problems+Serializing+Class+Which+Inherits+From+A+Generic+List.aspx</link>
      <pubDate>Thu, 13 Sep 2007 15:22:29 GMT</pubDate>
      <description>&lt;p&gt;
Usually I post solutions I've discovered or created to problems I've encountered.&amp;nbsp;
Now, I've run into a problem with the XmlSerializer and I need your help solving it.&amp;nbsp;&amp;nbsp;Scroll
to the bottom&amp;nbsp;for an update.
&lt;/p&gt;
&lt;h3&gt;The Problem
&lt;/h3&gt;
&lt;p&gt;
I have a class called Foobar.&amp;nbsp; Foobar inherits from List&amp;lt;string&amp;gt;, and contains
one member Title.&amp;nbsp; When I use the XmlSerializer to serialize and deserialize
Foobar, all the items in the List are make it through safely, but the member Title
does not.
&lt;/p&gt;
&lt;h3&gt;The Code
&lt;/h3&gt;
&lt;p&gt;
&lt;div class="wlWriterSmartContent" id="57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:cd057b70-45a7-4b72-a7db-655bf0456e1b" contenteditable="false" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px"&gt;&lt;pre style="background-color:#FFFF80;"&gt;
&lt;div&gt;
&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gt;&lt;span style="color: #0000FF; "&gt;using&lt;/span&gt;&lt;span style="color: #000000; "&gt; System; &lt;/span&gt;&lt;span style="color: #0000FF; "&gt;using&lt;/span&gt;&lt;span style="color: #000000; "&gt; System.Collections.Generic; &lt;/span&gt;&lt;span style="color: #0000FF; "&gt;using&lt;/span&gt;&lt;span style="color: #000000; "&gt; System.IO; &lt;/span&gt;&lt;span style="color: #0000FF; "&gt;using&lt;/span&gt;&lt;span style="color: #000000; "&gt; System.Xml.Serialization; &lt;/span&gt;&lt;span style="color: #0000FF; "&gt;internal&lt;/span&gt;&lt;span style="color: #000000; "&gt; &lt;/span&gt;&lt;span style="color: #0000FF; "&gt;class&lt;/span&gt;&lt;span style="color: #000000; "&gt; Program
{ &lt;/span&gt;&lt;span style="color: #0000FF; "&gt;private&lt;/span&gt;&lt;span style="color: #000000; "&gt; &lt;/span&gt;&lt;span style="color: #0000FF; "&gt;static&lt;/span&gt;&lt;span style="color: #000000; "&gt; &lt;/span&gt;&lt;span style="color: #0000FF; "&gt;void&lt;/span&gt;&lt;span style="color: #000000; "&gt; Main()
{ Foobar f &lt;/span&gt;&lt;span style="color: #000000; "&gt;=&lt;/span&gt;&lt;span style="color: #000000; "&gt; &lt;/span&gt;&lt;span style="color: #0000FF; "&gt;new&lt;/span&gt;&lt;span style="color: #000000; "&gt; Foobar();
f.Add(&lt;/span&gt;&lt;span style="color: #000000; "&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000; "&gt;1&lt;/span&gt;&lt;span style="color: #000000; "&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000; "&gt;);
f.Add(&lt;/span&gt;&lt;span style="color: #000000; "&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000; "&gt;2&lt;/span&gt;&lt;span style="color: #000000; "&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000; "&gt;);
f.Title &lt;/span&gt;&lt;span style="color: #000000; "&gt;=&lt;/span&gt;&lt;span style="color: #000000; "&gt; &lt;/span&gt;&lt;span style="color: #000000; "&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000; "&gt;Title&lt;/span&gt;&lt;span style="color: #000000; "&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000; "&gt;;
MemoryStream mem &lt;/span&gt;&lt;span style="color: #000000; "&gt;=&lt;/span&gt;&lt;span style="color: #000000; "&gt; &lt;/span&gt;&lt;span style="color: #0000FF; "&gt;new&lt;/span&gt;&lt;span style="color: #000000; "&gt; MemoryStream();
XmlSerializer xs &lt;/span&gt;&lt;span style="color: #000000; "&gt;=&lt;/span&gt;&lt;span style="color: #000000; "&gt; &lt;/span&gt;&lt;span style="color: #0000FF; "&gt;new&lt;/span&gt;&lt;span style="color: #000000; "&gt; XmlSerializer(&lt;/span&gt;&lt;span style="color: #0000FF; "&gt;typeof&lt;/span&gt;&lt;span style="color: #000000; "&gt;(Foobar));
xs.Serialize(mem, f); mem.Seek(&lt;/span&gt;&lt;span style="color: #000000; "&gt;0&lt;/span&gt;&lt;span style="color: #000000; "&gt;, &lt;/span&gt;&lt;span style="color: #000000; "&gt;0&lt;/span&gt;&lt;span style="color: #000000; "&gt;);
mem.Seek(&lt;/span&gt;&lt;span style="color: #000000; "&gt;0&lt;/span&gt;&lt;span style="color: #000000; "&gt;, &lt;/span&gt;&lt;span style="color: #000000; "&gt;0&lt;/span&gt;&lt;span style="color: #000000; "&gt;);
Foobar deser &lt;/span&gt;&lt;span style="color: #000000; "&gt;=&lt;/span&gt;&lt;span style="color: #000000; "&gt; (Foobar)xs.Deserialize(mem);
Console.WriteLine(deser); } } [Serializable] &lt;/span&gt;&lt;span style="color: #0000FF; "&gt;public&lt;/span&gt;&lt;span style="color: #000000; "&gt; &lt;/span&gt;&lt;span style="color: #0000FF; "&gt;class&lt;/span&gt;&lt;span style="color: #000000; "&gt; Foobar
: List&lt;/span&gt;&lt;span style="color: #000000; "&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #0000FF; "&gt;string&lt;/span&gt;&lt;span style="color: #000000; "&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000; "&gt; { &lt;/span&gt;&lt;span style="color: #0000FF; "&gt;public&lt;/span&gt;&lt;span style="color: #000000; "&gt; &lt;/span&gt;&lt;span style="color: #0000FF; "&gt;string&lt;/span&gt;&lt;span style="color: #000000; "&gt; Title;
}&lt;/span&gt;
&lt;/div&gt;
&lt;/pre&gt;
&lt;/div&gt;
&gt;
&lt;h3&gt;The Solution
&lt;/h3&gt;
&lt;p&gt;
One way I came up with to solve this problem is make Foobar not inherit from List&amp;lt;string&amp;gt;,
and make a member called Items that is a List&amp;lt;string&amp;gt;, but that's kind of hokey.&amp;nbsp;
I've searched Google and can't find a solution.&amp;nbsp; So I'm appealing to my dear
readers for a better solution.
&lt;/p&gt;
&lt;h3&gt;Update
&lt;/h3&gt;
&lt;p&gt;
Looks like I was being way to specific in my Google search, searching on "XmlSerializer
List" gives us this &lt;a title="http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=566175&amp;amp;SiteID=1" href="http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=566175&amp;amp;SiteID=1"&gt;http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=566175&amp;amp;SiteID=1&lt;/a&gt;&amp;nbsp;as
the first item.&amp;nbsp; In the post, 
&lt;/p&gt;
&lt;p&gt;
Elena Kharitidi says "XmlSerializer does not serialize any members if a collection.
Only collection items get serialized. This is by design, basically a decision was
made to handle collections as arrays not as classes with multiple properties, so collections
should look like arrays on the wire, therefore they do not have any members other
then collection items, and can be “flattened” by adding the [XmlElement] to the member
of the ICollection type." 
&lt;p&gt;
Long and short, I need to use the hokey solution I came up with (or something like
it), or write my own serializer.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.milkcarton.com/blog/aggbug.ashx?id=9fe3849a-f364-43ea-acb4-b100e991e1cc" /&gt;</description>
      <comments>http://www.milkcarton.com/blog/CommentView,guid,9fe3849a-f364-43ea-acb4-b100e991e1cc.aspx</comments>
      <category>.NET</category>
      <category>C#</category>
      <category>XmlSerializer</category>
    </item>
  </channel>
</rss>