<?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"
	>
<channel>
	<title>Comments on: Serializing an IDictionary object</title>
	<atom:link href="http://www.mattberther.com/2004/06/14/serializing-an-idictionary-object/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.mattberther.com/2004/06/14/serializing-an-idictionary-object/</link>
	<description>Agile Manager and Occasional Code Monkey</description>
	<pubDate>Sat, 06 Sep 2008 05:14:42 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6</generator>
		<item>
		<title>By: pepz</title>
		<link>http://www.mattberther.com/2004/06/14/serializing-an-idictionary-object/#comment-165884</link>
		<dc:creator>pepz</dc:creator>
		<pubDate>Sun, 22 Jun 2008 00:49:36 +0000</pubDate>
		<guid isPermaLink="false">http://www.mattberther.com/blog/?p=487#comment-165884</guid>
		<description>I think the function 'ReadXml' needs an extra function call to 'reader.ReadEndElement()' to fully close out the working node.  This should allow it to work within larger object graphs as well.</description>
		<content:encoded><![CDATA[<p>I think the function &#8216;ReadXml&#8217; needs an extra function call to &#8216;reader.ReadEndElement()&#8217; to fully close out the working node.  This should allow it to work within larger object graphs as well.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Nikhil Shah</title>
		<link>http://www.mattberther.com/2004/06/14/serializing-an-idictionary-object/#comment-165587</link>
		<dc:creator>Nikhil Shah</dc:creator>
		<pubDate>Fri, 21 Mar 2008 07:26:41 +0000</pubDate>
		<guid isPermaLink="false">http://www.mattberther.com/blog/?p=487#comment-165587</guid>
		<description>This works very well,
gooood work , 
data gets serialized successfully, 
but does not gets deserialize !!!
my problem is smwht like this, I have Four  hashtable that stores the key n value pair,
on Form I have 4 combobox n 4 respective textbox ,
I wnt to populate the Key in combobx n Its RESPECTIVE value in textbox
on the selectedindex event of the combx, I where I write a code
I gets an error, tht there is a problem with XML document !!!
this XMl doc is well-formed, No-bugs thr..
this means tht the data is nt deserializing ...

If you get this, plz help me out m stuck in this prblm since last 2 days :(</description>
		<content:encoded><![CDATA[<p>This works very well,<br />
gooood work ,<br />
data gets serialized successfully,<br />
but does not gets deserialize !!!<br />
my problem is smwht like this, I have Four  hashtable that stores the key n value pair,<br />
on Form I have 4 combobox n 4 respective textbox ,<br />
I wnt to populate the Key in combobx n Its RESPECTIVE value in textbox<br />
on the selectedindex event of the combx, I where I write a code<br />
I gets an error, tht there is a problem with XML document !!!<br />
this XMl doc is well-formed, No-bugs thr..<br />
this means tht the data is nt deserializing &#8230;</p>
<p>If you get this, plz help me out m stuck in this prblm since last 2 days :(</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Simon</title>
		<link>http://www.mattberther.com/2004/06/14/serializing-an-idictionary-object/#comment-155512</link>
		<dc:creator>Simon</dc:creator>
		<pubDate>Wed, 10 Oct 2007 16:16:46 +0000</pubDate>
		<guid isPermaLink="false">http://www.mattberther.com/blog/?p=487#comment-155512</guid>
		<description>Hmm, that comment has mangled some of the XML tags that were in the code, the method won't work as it is shown on this page</description>
		<content:encoded><![CDATA[<p>Hmm, that comment has mangled some of the XML tags that were in the code, the method won&#8217;t work as it is shown on this page</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Simon</title>
		<link>http://www.mattberther.com/2004/06/14/serializing-an-idictionary-object/#comment-155511</link>
		<dc:creator>Simon</dc:creator>
		<pubDate>Wed, 10 Oct 2007 16:15:22 +0000</pubDate>
		<guid isPermaLink="false">http://www.mattberther.com/blog/?p=487#comment-155511</guid>
		<description>Everything being serialized as a string, and the non-recursive nature of the serialization make this kind of useless. I've ended up writing a couple of helper methods that will serialize and deserialize nested IDictionary objects too.

&lt;code&gt;
	public static StringBuilder Serialize(StringBuilder sb, object obj)
        {
            if (typeof(IDictionary).IsAssignableFrom(obj.GetType()))
            {
                sb.Append("");
                foreach (object key in ((IDictionary)obj).Keys)
                {
                    sb.Append("");
                    Serialize(sb, ((IDictionary)obj)[key]);
                    sb.Append("");
                }
                sb.Append("");
            }
            else
            {
                StringWriter sw = new StringWriter(sb);
                XmlSerializer xs = new XmlSerializer(obj.GetType());                
                sb.Append("");
                //need a bodge in here to deal with unnamed datatables
                xs.Serialize(sw, obj);
                sb.Append("");
            }

            return sb.Replace("", "");
        }

        public static object Deserialize(string s)
        {
            Object obj = new Object();

            XmlDocument xml = new XmlDocument();
            xml.LoadXml(s);

            XmlNode node = xml.ChildNodes[0];
            if (node.Name.Contains("IDictionary_"))
            {
                obj = Activator.CreateInstance(Type.GetType(XmlConvert.DecodeName(node.Name.Replace("IDictionary_", ""))));
                foreach (XmlNode child in node.ChildNodes)
                    ((IDictionary)obj).Add(child.Name.Replace("Key_", ""), Deserialize(child.InnerXml));
            }
            else if (node.Name.Contains("Type_"))
            {
                StringReader sr = new StringReader("" + node.InnerXml);
                XmlSerializer xs = new XmlSerializer(Type.GetType(XmlConvert.DecodeName(node.Name.Replace("Type_", ""))));
                obj = xs.Deserialize(sr);
            }

            return obj;
        }
&lt;/code&gt;

Use it like this:
	&lt;code&gt;
Hashtable ht1 = new Hashtable();
        ht1["1"] = 1;
        ht1["2"] = 2;

        Hashtable ht2 = new Hashtable();
        ht2["a"] = "a";
        ht2["b"] = "b";

        ht1["3"] = ht2;

        Serialize(new StringBuilder(), ht1);
&lt;/code&gt;</description>
		<content:encoded><![CDATA[<p>Everything being serialized as a string, and the non-recursive nature of the serialization make this kind of useless. I&#8217;ve ended up writing a couple of helper methods that will serialize and deserialize nested IDictionary objects too.</p>
<p><code><br />
	public static StringBuilder Serialize(StringBuilder sb, object obj)<br />
        {<br />
            if (typeof(IDictionary).IsAssignableFrom(obj.GetType()))<br />
            {<br />
                sb.Append("");<br />
                foreach (object key in ((IDictionary)obj).Keys)<br />
                {<br />
                    sb.Append("");<br />
                    Serialize(sb, ((IDictionary)obj)[key]);<br />
                    sb.Append(&#8221;");<br />
                }<br />
                sb.Append(&#8221;");<br />
            }<br />
            else<br />
            {<br />
                StringWriter sw = new StringWriter(sb);<br />
                XmlSerializer xs = new XmlSerializer(obj.GetType());<br />
                sb.Append(&#8221;");<br />
                //need a bodge in here to deal with unnamed datatables<br />
                xs.Serialize(sw, obj);<br />
                sb.Append(&#8221;");<br />
            }</p>
<p>            return sb.Replace(&#8221;", &#8220;&#8221;);<br />
        }</p>
<p>        public static object Deserialize(string s)<br />
        {<br />
            Object obj = new Object();</p>
<p>            XmlDocument xml = new XmlDocument();<br />
            xml.LoadXml(s);</p>
<p>            XmlNode node = xml.ChildNodes[0];<br />
            if (node.Name.Contains(&#8221;IDictionary_&#8221;))<br />
            {<br />
                obj = Activator.CreateInstance(Type.GetType(XmlConvert.DecodeName(node.Name.Replace(&#8221;IDictionary_&#8221;, &#8220;&#8221;))));<br />
                foreach (XmlNode child in node.ChildNodes)<br />
                    ((IDictionary)obj).Add(child.Name.Replace(&#8221;Key_&#8221;, &#8220;&#8221;), Deserialize(child.InnerXml));<br />
            }<br />
            else if (node.Name.Contains(&#8221;Type_&#8221;))<br />
            {<br />
                StringReader sr = new StringReader(&#8221;" + node.InnerXml);<br />
                XmlSerializer xs = new XmlSerializer(Type.GetType(XmlConvert.DecodeName(node.Name.Replace(&#8221;Type_&#8221;, &#8220;&#8221;))));<br />
                obj = xs.Deserialize(sr);<br />
            }</p>
<p>            return obj;<br />
        }<br />
</code></p>
<p>Use it like this:<br />
	<code><br />
Hashtable ht1 = new Hashtable();<br />
        ht1["1"] = 1;<br />
        ht1["2"] = 2;</p>
<p>        Hashtable ht2 = new Hashtable();<br />
        ht2["a"] = &#8220;a&#8221;;<br />
        ht2["b"] = &#8220;b&#8221;;</p>
<p>        ht1["3"] = ht2;</p>
<p>        Serialize(new StringBuilder(), ht1);<br />
</code></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Wil</title>
		<link>http://www.mattberther.com/2004/06/14/serializing-an-idictionary-object/#comment-153543</link>
		<dc:creator>Wil</dc:creator>
		<pubDate>Thu, 04 Oct 2007 12:41:50 +0000</pubDate>
		<guid isPermaLink="false">http://www.mattberther.com/blog/?p=487#comment-153543</guid>
		<description>The serialization loses what I would consider one of the main advantages of the Hashtable and that is the type of the value.

A DateTime value is deserialized as a string and not a DateTime.</description>
		<content:encoded><![CDATA[<p>The serialization loses what I would consider one of the main advantages of the Hashtable and that is the type of the value.</p>
<p>A DateTime value is deserialized as a string and not a DateTime.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Daniel</title>
		<link>http://www.mattberther.com/2004/06/14/serializing-an-idictionary-object/#comment-64411</link>
		<dc:creator>Daniel</dc:creator>
		<pubDate>Fri, 04 May 2007 08:40:05 +0000</pubDate>
		<guid isPermaLink="false">http://www.mattberther.com/blog/?p=487#comment-64411</guid>
		<description>
     
</description>
		<content:encoded><![CDATA[<br />
]]></content:encoded>
	</item>
	<item>
		<title>By: Daniel</title>
		<link>http://www.mattberther.com/2004/06/14/serializing-an-idictionary-object/#comment-64409</link>
		<dc:creator>Daniel</dc:creator>
		<pubDate>Fri, 04 May 2007 08:39:29 +0000</pubDate>
		<guid isPermaLink="false">http://www.mattberther.com/blog/?p=487#comment-64409</guid>
		<description>this is the xml, i don't know why it wasn't saved:
&lt;code&gt;
 This is how the xml must look like:
 
 
   
   
     
   
 
&lt;/code&gt;</description>
		<content:encoded><![CDATA[<p>this is the xml, i don&#8217;t know why it wasn&#8217;t saved:<br />
<code><br />
 This is how the xml must look like:</p>
<p></code></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Daniel</title>
		<link>http://www.mattberther.com/2004/06/14/serializing-an-idictionary-object/#comment-64407</link>
		<dc:creator>Daniel</dc:creator>
		<pubDate>Fri, 04 May 2007 08:38:01 +0000</pubDate>
		<guid isPermaLink="false">http://www.mattberther.com/blog/?p=487#comment-64407</guid>
		<description>hi, i have this class, which has all the collection properties and methods. in it i have a property, Total, which returns the number of elements.
&lt;code&gt;public class PostcodeCollection : CollectionBase
{

        // .... other collection's stuff

	[XmlAttribute( "Total" )]
	public int Total
	{
		get { return List.Count; }
		set
		{}
	}

}
&lt;/code&gt;
this collection is a property in another class:
&lt;code&gt;
[XmlRoot("PostCodes" )]
public class PostcodeReport
{
        // ...other properties

	[XmlArray("Updated")]
	[XmlArrayItem(typeof(PostCodeLine), ElementName="PostCode")]
	public PostcodeCollection Updated
	{
		get { return this.updated; }
	}
}
&lt;/code&gt; 
i serialize the collection, and all is ok, but i want to add the "Total" property to the xml:
 This is how the xml must look like:
 
 
   
   
     
   
 

i don't know how to apply your example, any thoughts?
thanks,
Daniel</description>
		<content:encoded><![CDATA[<p>hi, i have this class, which has all the collection properties and methods. in it i have a property, Total, which returns the number of elements.<br />
<code>public class PostcodeCollection : CollectionBase<br />
{</p>
<p>        // .... other collection's stuff</p>
<p>	[XmlAttribute( "Total" )]<br />
	public int Total<br />
	{<br />
		get { return List.Count; }<br />
		set<br />
		{}<br />
	}</p>
<p>}<br />
</code><br />
this collection is a property in another class:<br />
<code><br />
[XmlRoot("PostCodes" )]<br />
public class PostcodeReport<br />
{<br />
        // &#8230;other properties</p>
<p>	[XmlArray("Updated")]<br />
	[XmlArrayItem(typeof(PostCodeLine), ElementName="PostCode")]<br />
	public PostcodeCollection Updated<br />
	{<br />
		get { return this.updated; }<br />
	}<br />
}<br />
</code><br />
i serialize the collection, and all is ok, but i want to add the &#8220;Total&#8221; property to the xml:<br />
 This is how the xml must look like:</p>
<p>i don&#8217;t know how to apply your example, any thoughts?<br />
thanks,<br />
Daniel</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Victor Bartel</title>
		<link>http://www.mattberther.com/2004/06/14/serializing-an-idictionary-object/#comment-547</link>
		<dc:creator>Victor Bartel</dc:creator>
		<pubDate>Wed, 31 May 2006 14:37:14 +0000</pubDate>
		<guid isPermaLink="false">http://www.mattberther.com/blog/?p=487#comment-547</guid>
		<description>Greetings,

Thank you for this wonderful article, it helped me so much. I permit me ask you about one problem that I’ve meet. I use .NET 1.1, your class works correctly in web methods, but I have a problem to pass Serialized HashTable from web method to some asp .net application, using web references. When VS environment generate proxy class, it define your DictionarySerializer like a DataSet. For example for next web method:  

[Webmethod]
 public DictionarySerializer Test()
{
  ………
  return new DictionarySerializer(MyHashTable);
}

VS will generate something like that in a proxy class:
        
        public System.Data.DataSet Test() {
            object[] results = this.Invoke("Test", new object[0]);
            return ((System.Data.DataSet)(results[0]));
        }
Without a schema specification the system define serializeble object like a DataSet. What I need to correct this problem, can you help me to do that? Thank you in advance.

Best regards
Victor Bartel

</description>
		<content:encoded><![CDATA[<p>Greetings,</p>
<p>Thank you for this wonderful article, it helped me so much. I permit me ask you about one problem that I’ve meet. I use .NET 1.1, your class works correctly in web methods, but I have a problem to pass Serialized HashTable from web method to some asp .net application, using web references. When VS environment generate proxy class, it define your DictionarySerializer like a DataSet. For example for next web method:  </p>
<p>[Webmethod]<br />
 public DictionarySerializer Test()<br />
{<br />
  ………<br />
  return new DictionarySerializer(MyHashTable);<br />
}</p>
<p>VS will generate something like that in a proxy class:</p>
<p>        public System.Data.DataSet Test() {<br />
            object[] results = this.Invoke(&#8221;Test&#8221;, new object[0]);<br />
            return ((System.Data.DataSet)(results[0]));<br />
        }<br />
Without a schema specification the system define serializeble object like a DataSet. What I need to correct this problem, can you help me to do that? Thank you in advance.</p>
<p>Best regards<br />
Victor Bartel</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dave J Smith</title>
		<link>http://www.mattberther.com/2004/06/14/serializing-an-idictionary-object/#comment-546</link>
		<dc:creator>Dave J Smith</dc:creator>
		<pubDate>Tue, 09 May 2006 08:14:38 +0000</pubDate>
		<guid isPermaLink="false">http://www.mattberther.com/blog/?p=487#comment-546</guid>
		<description>For those who are struggling to find the correct namespaces:

using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;
using System.Collections;
using System.IO;

are required.
Dave.</description>
		<content:encoded><![CDATA[<p>For those who are struggling to find the correct namespaces:</p>
<p>using System.Xml;<br />
using System.Xml.Schema;<br />
using System.Xml.Serialization;<br />
using System.Collections;<br />
using System.IO;</p>
<p>are required.<br />
Dave.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
