XML Serializable Generic Dictionary

Posted Wednesday, May 03, 2006 12:13 PM by pwelter34

XML Serializable Generic Dictionary

For some reason, the generic Dictionary in .net 2.0 is not XML serializable.  The following code snippet is a xml serializable generic dictionary.  The dictionary is serialzable by implementing the IXmlSerializable interface. 

    using System;

    using System.Collections.Generic;

    using System.Text;

    using System.Xml.Serialization;

 

    [XmlRoot("dictionary")]

    public class SerializableDictionary<TKey, TValue>

        : Dictionary<TKey, TValue>, IXmlSerializable

    {

        #region IXmlSerializable Members

        public System.Xml.Schema.XmlSchema GetSchema()

        {

            return null;

        }

 

        public void ReadXml(System.Xml.XmlReader reader)

        {

            XmlSerializer keySerializer = new XmlSerializer(typeof(TKey));

            XmlSerializer valueSerializer = new XmlSerializer(typeof(TValue));

 

            bool wasEmpty = reader.IsEmptyElement;

            reader.Read();

 

            if (wasEmpty)

                return;

 

            while (reader.NodeType != System.Xml.XmlNodeType.EndElement)

            {

                reader.ReadStartElement("item");

 

                reader.ReadStartElement("key");

                TKey key = (TKey)keySerializer.Deserialize(reader);

                reader.ReadEndElement();

 

                reader.ReadStartElement("value");

                TValue value = (TValue)valueSerializer.Deserialize(reader);

                reader.ReadEndElement();

 

                this.Add(key, value);

 

                reader.ReadEndElement();

                reader.MoveToContent();

            }

            reader.ReadEndElement();

        }

 

        public void WriteXml(System.Xml.XmlWriter writer)

        {

            XmlSerializer keySerializer = new XmlSerializer(typeof(TKey));

            XmlSerializer valueSerializer = new XmlSerializer(typeof(TValue));

 

            foreach (TKey key in this.Keys)

            {

                writer.WriteStartElement("item");

 

                writer.WriteStartElement("key");

                keySerializer.Serialize(writer, key);

                writer.WriteEndElement();

 

                writer.WriteStartElement("value");

                TValue value = this[key];

                valueSerializer.Serialize(writer, value);

                writer.WriteEndElement();

 

                writer.WriteEndElement();

            }

        }

        #endregion

    }

Update: Fixed bug Justin pointed out by adding an extra reader.ReadEndElement() and by checking the IsEmptyElement property.

Filed under:

Comments

# re: XML Serializable Generic Dictionary

Wednesday, May 03, 2006 2:38 PM by Justin-Josef Angel

I wrote about this issue five months back at and added a CodeSmith generation template:
http://www.justinangel.net/e/CommentView,guid,55167d3d-b4ac-40be-9909-402e3783edab.aspx

BTW, your code is buggy. If you put this Generic dictionary as part of another class it will not desirilize properly. The read method is missing a few reads so the XmlReader will not be set correctly for any items in the XML after this Generic dictionary.

# re: XML Serializable Generic Dictionary

Wednesday, May 03, 2006 2:44 PM by Paul Welter

Justin,

Do you have an example of what will cause it to fail? I've done a lot of testing but must have missed a use case. Thanks for the heads up.

~ Paul

# re: XML Serializable Generic Dictionary

Wednesday, May 03, 2006 4:12 PM by Justin-Josef Angel

Try and create a class with the SerializableAttribute and properties of type string, SerializableDictionary, string (in that order).

Make a webservice method that returns this class (with some content, meaning that string should have a value and the dirctionary should have at least two values).

Look the the XML the webservice generates and see it's OK.

Create a proxy to the webservice and call the webservice method you just created. The class you deserialize will have no value for the string that comes after the SerializableDictionary. (At least i think it won't). If i'm right and that issue does exist it's because you don't read the </dictionary> element at the end of ReadXml.

# re: XML Serializable Generic Dictionary

Wednesday, May 03, 2006 4:20 PM by Paul Welter

Ok, I fixed the issue. I got about 30 test cases for this now. Should be fairly solid. No guarantees of course. :)

~ Paul

# re: XML Serializable Generic Dictionary

Monday, May 15, 2006 4:04 AM by Ward Bekker

Hi Paul,

Thanks for creating this, saved me time!

Ward

# re: XML Serializable Generic Dictionary

Monday, May 15, 2006 4:08 PM by Brian

This doesn't seem to work if you have a dictionary of custom types.

I.E. SerializableDictionary<int, MyObject>

# re: XML Serializable Generic Dictionary

Monday, May 15, 2006 4:11 PM by Paul Welter

Brian,

It should work as long as MyObject is Xml Serializable. Try to serialize MyObject first to see if it works.

~ Paul

# re: XML Serializable Generic Dictionary

Monday, May 15, 2006 4:13 PM by Brian

Nevermind! Ignore previous comment.

# re: XML Serializable Generic Dictionary

Monday, May 15, 2006 4:14 PM by Brian

Paul it does, my goof. Thanks this is very helpful.

# re: XML Serializable Generic Dictionary

Monday, August 28, 2006 10:45 AM by Mark

Thanks, Paul. This is really neat. I have a WebMethod returning a SerializableDictionary. This works fine when I view the service in a browser, & call the method there, but somehow does not work from a fat-client. In fact the generated wsdl does not show the response-type as above. So, am not sure whether i need to do anything special to get it in my proxy?

# re: XML Serializable Generic Dictionary

Monday, September 25, 2006 4:36 AM by Matthias

Thanks for your code-example, but I get an error-message, by using this solution. I define the SerializableDictionary, the key (an enumeration) and value (a struct with two items from datatype string) in an own ClassLibrary. When I use a method in a WebService1 like this public SerializableDictionary testSD1() { SerializableDictionary sd = new SerializableDictionary(); sd.Add(eKeys.key1, new sValues("value1", "value2")); return sd; } It works. When I test it, i get an XML-Document with all Information. When I use a second method in a WebService2: [WebMethod] public System.Xml.XmlDocument testSD4() { localhost.Service ws = new localhost.Service(); datatype.SerializableDictionary sd; System.Data.DataSet ds = ws.testSD1(); System.Xml.XmlDocument xd = new System.Xml.XmlDocument(); xd.LoadXml(ds.GetXml()); return xd; } I recieve just this: Can you help me please? Thanks.

# re: XML Serializable Generic Dictionary

Monday, October 16, 2006 7:28 AM by Pablo

Great piece of code. Thanks very much.

# re: XML Serializable Generic Dictionary

Thursday, October 26, 2006 8:48 PM by Srivalli

Hi, Thanks for the code... I appreciate your effort. I'm having some problems though.... 1. When I used the code as is, I got this error ... [SerializationException: Type 'Microsoft.Exchange.HostedServices.Clients.Utilities.SerializableDictionary`2[[System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[Microsoft.Exchange.HostedServices.Clients.Membership.User, Microsoft.Exchange.HostedServices.Clients.Membership, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null]]' in Assembly 'Microsoft.Exchange.HostedServices.Clients.Utilities, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' is not marked as serializable.] I marked it as [Serializable] and then I got the following error... {"The constructor to deserialize an object of type 'Microsoft.Exchange.HostedServices.Clients.Utilities.SerializableDictionary`2[System.String,Microsoft.Exchange.HostedServices.Clients.Membership.User]' was not found."} Then I added the following... protected SerializableDictionary(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context) { } Now there are no errors, but the serialization/deserialization didn't seem to work at all. ReadXml and WriteXml methods were never called... BTW, I'm storing SerializableDictionary object in session as follows... HttpContext.Current.Session["UserAccounts"] = new SerializableDictionary(); ((SerializableDictionary)HttpContext.Current.Session["UserAccounts"])[emailAddress] = user; I'm stuck... Any help/feedback is appreciated. Thank you, Srivalli

# re: XML Serializable Generic Dictionary

Friday, October 27, 2006 6:31 AM by Tatworth

A sample peice of C# code to exercise this in unit tests would appreciated.

# Web 2.0 &raquo; Blog Archives &raquo; Merriam-Webster&#8217;s Spanish-English Dictionary 2.0 from email marketing

# Web 2.0 &raquo; Blog Archives &raquo; Smartphone.net - Windows Mobile Smartphone Software, News, Etc

# Serializable Generic Dictionary

Thursday, June 07, 2007 2:09 AM by Telligent.Interns[0] = "Ryan Hoffman";

In CommunityServer Enterprise Reporting ,we&#39;re using a neat web service to make our architecture

# Web 2.0 &raquo; Blog Archives &raquo; Find Shorter Oxford English Dictionary Windows Version 2.0

Pingback from  Web 2.0  &raquo; Blog Archives   &raquo; Find Shorter Oxford English Dictionary Windows Version 2.0

# re: XML Serializable Generic Dictionary

Sunday, July 15, 2007 6:55 PM by Balslev

Just want to let you know that your little piece works wonders, and saved me quite a few headaches.

Thanks for sharing!

# JSON in C# (Part 1)

Wednesday, August 01, 2007 12:53 AM by Nimble Coder

JSON in C# (Part 1)

# Code Pagoda &raquo; Blog Archive &raquo; Pass Dictionary Object Params To Your Web Service

Pingback from  Code Pagoda  &raquo; Blog Archive   &raquo; Pass Dictionary Object Params To Your Web Service

# re: XML Serializable Generic Dictionary

Wednesday, September 12, 2007 10:03 AM by Gary

Great snippet for sure! Thanks for sharing it with us! I am having a problem, however, I hoped I could get some help with:

I have a web service with a webmethod (where I expect a SerializableDictionary<string,string> as a parameter). When I generate the client-proxy the reference.cs auto-generated class shows that parameter to be System.Data.Dataset instead of MyNamespace.SerializableDictionary<string,string>. Any attempts thus far to generate the schema manually has lead to dead ends ...

How can I (other than manually) Specify the schema for serializing the SerializableDisctionary so that the client "sees" a serializable dictionary as the parameter to this web method?

# re: XML Serializable Generic Dictionary

Wednesday, September 19, 2007 8:58 PM by Thomas

@Srivalli

You have to call the base constructor of the Dictionary class it's inheriting to get it to work. Adding the following constructors should get it to work exactly like the real Dictionary implementation:

       public SerializableDictionary()

           : base()

       {

       }

       public SerializableDictionary(IDictionary<TKey, TValue> dictionary)

           : base(dictionary)

       {

       }

       public SerializableDictionary(IEqualityComparer<TKey> comparer)

           : base(comparer)

       {

       }

       public SerializableDictionary(int capacity)

           : base(capacity)

       {

       }

       public SerializableDictionary(IDictionary<TKey, TValue> dictionary, IEqualityComparer<TKey> comparer)

           : base(dictionary, comparer)

       {

       }

       public SerializableDictionary(int capacity, IEqualityComparer<TKey> comparer)

           : base(capacity, comparer)

       {

       }

       protected SerializableDictionary(SerializationInfo info, StreamingContext context)

           : base(info, context)

       {

       }

# re: XML Serializable Generic Dictionary

Friday, September 21, 2007 5:25 PM by Chad

Thanks for putting this out there.  Worked like a charm!

# re: XML Serializable Generic Dictionary

Sunday, October 14, 2007 11:07 AM by Code

Pingback from code-news.blogspot.com

# re: XML Serializable Generic Dictionary

Friday, October 26, 2007 6:53 PM by Brian Miedlar

@gary

You must bind an XmlSchemaProvider in order to get the type to change on a remote web service.

I built upon Paul's version for working VB code using a dictionary of type (string, object)

miedlar.com/.../serializabledictionary

<a href='miedlar.com/.../serializabledictionary'>here</a>

# huseyint.com &raquo; XML Serializable Generic Dictionary tipi

Sunday, December 02, 2007 9:50 AM by huseyint.com » XML Serializable Generic Dictionary tipi

Pingback from  huseyint.com &raquo; XML Serializable Generic Dictionary tipi

# re: XML Serializable Generic Dictionary

Thursday, December 13, 2007 5:50 AM by alastair green

Hello,

I get an exception when I try to serialize a property that uses this.  The problem seems to be that I have decorated the property with XmlArrayAttribute and XmlArrayItemAttribute.  Reading the msdn docs, this should be valid for any type that implements IEnumerable, as your class inherits from Dictionary I should be ok.  Any suggestions?

SerializableDictionary<string,object> _objectTypes = new SerializableDictionary<string,object>();

       [XmlArray("object_definitions")]

       [XmlArrayItem(typeof(UiConfiguratorPlugin), ElementName = "object_type", Namespace = XMLNamespaces.UiConfiguratorPlugin)]

       [XmlArrayItem(typeof(ProtocolRunnerPlugIn), ElementName = "object_type", Namespace = XMLNamespaces.ProtocolRunnerPlugIn)]

       //[XmlAnyElement("object_type")]      

       public SerializableDictionary<string, object> ObjectTypes

       {

           get { return _objectTypes; }

           set { _objectTypes = value; }

       }

# re: XML Serializable Generic Dictionary

Thursday, December 13, 2007 10:38 AM by Adam

Sorry if its been posted already, but if you are trying to put this collection into ViewState, you will need to explicitly implement a constructor on the Dictionary for it to deserialise properly.   I implemented them all explicitly as pass-through methods, simply calling base.

Please refer to:

michhes.blogspot.com/.../generic-dictionary-wrapper-class-cannot.html

Thanks.

# re: XML Serializable Generic Dictionary

Monday, December 31, 2007 11:40 AM by Yury

At string writer.WriteStartElement("item");

InvalidOperationException :

Token StartElement in state EndRootElement would result in an invalid XML document. Make sure that the ConformanceLevel setting is set to ConformanceLevel.Fragment or ConformanceLevel.Auto if you want to write an XML fragment.

# re: XML Serializable Generic Dictionary

Wednesday, January 09, 2008 4:56 PM by Tim

In my thin client i'm unable to properly parse the xml generated by this class. It treats the SerializedDictionary object as a DataSet, but the data set is empty.

Help!

# re: XML Serializable Generic Dictionary

Friday, January 18, 2008 7:14 AM by Alex

The class does not work for properties and for subclasses:

public class Test

{

 private SerializableDictionary<string, string> _sd = new SerializableDictionary<string, string>();

 public Test() { _sd.Add("name", "value"); }

 public SerializableDictionary<string, string> SD { get { return _sd; } }

}

Test t = new Test();

XmlSerializer serializer = new XmlSerializer(t.GetType());

StreamWriter writer = new StreamWriter("c:\\t.xml");

serializer.Serialize(writer, t);

writer.Close();

Outputs only

 <?xml version="1.0" encoding="utf-8" ?>

- <Test xmlns:xsi="www.w3.org/.../XMLSchema-instance" xmlns:xsd="www.w3.org/.../XMLSchema">

 </Test>

# re: XML Serializable Generic Dictionary

Friday, January 18, 2008 11:52 AM by Alex

Important: readonly public variables and get-only properties cannot be serialized to XML!

# re: XML Serializable Generic Dictionary

Wednesday, January 30, 2008 7:19 PM by Gleb

You are a genious!

Thanks a lot.

# re: XML Serializable Generic Dictionary

Friday, February 01, 2008 3:48 PM by Sowokie

Im having a problem using this with TryGetValue (trying to ensure thread safety).

I have a SiteMapDataSource that i get a System.NullReferenceException on (CurrentNode==null).

       protected void menu_MenuItemDataBound(Object sender, MenuEventArgs e)

       {

          if (e.Item.Text == SiteMapDataSource 1.Provider.CurrentNode.Title)

           {

               e.Item.Text = "<strong>" + e.Item.Text + "</strong>";

           }

       }

But if i just skip the first if() everything works like a charm.

string tempCountryCode="uk";

bool contains;

           if (page.CountryCodeDictionary.TryGetValue(tempCountryCode.ToLower(), out contains))

           {

               if (SupportRoles.isAdmin(HttpContext.Current.Request.LogonUserIdentity) || page.CountryCodeDictionary[tempCountryCode])

               {

                   return true;

               }

               else

               {

                   return false;

               }

           }

           else

           {

               return false;

           }

Would be great if somebody knew what was causing this.

# re: XML Serializable Generic Dictionary

Wednesday, February 13, 2008 4:19 PM by Jason

This works great.  However, I am having one problem, when returning a Serializable dictionary from my web service, it is being treated as a DataSet.

Is there any way to have this serialize as a two dimensional array instead of a dataset.  This would also ensure that any type contained in the dictionary will have its type definition included in both the wsdl and the reference.cs web service proxy which is created on the client side.

# re: XML Serializable Generic Dictionary

Monday, February 18, 2008 6:53 PM by Gleb

I am having problems with this simple routine:

   <WebMethod()> Public Function GetBorough(ByVal iCompID As Integer) As SerializableDictionary(Of String, String)

       Dim oSearchResult As New SerializableDictionary(Of String, String)

       oSearchResult.Add("1", "one")

       Return oSearchResult

   End Function

----------------------------------------

Error is:

- The request failed with the error message:

--

System.InvalidOperationException: The top XML element 'dictionary' from namespac

e 'http://tempuri.org/' references distinct types SerializableDictionary`2[Syste

m.String,Photo] and SerializableDictionary`2[System.String,System.String]. Use X

ML attributes to specify another XML name or namespace for the element or types.

  at System.Xml.Serialization.XmlReflectionImporter.ReconcileAccessor(Accessor

accessor, NameTable accessors)

  at System.Xml.Serialization.XmlReflectionImporter.ImportElement(TypeModel mod

el, XmlRootAttribute root, String defaultNamespace)

  at System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(Type type

, XmlRootAttribute root, String defaultNamespace)

  at System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(Type type

, XmlRootAttribute root)

  at System.Web.Services.Description.MimeXmlReflector.ReflectReturn()

  at System.Web.Services.Description.HttpProtocolReflector.ReflectMimeReturn()

  at System.Web.Services.Description.HttpPostProtocolReflector.ReflectMethod()

  at System.Web.Services.Description.ProtocolReflector.ReflectBinding(Reflected

Binding reflectedBinding)

  at System.Web.Services.Description.ProtocolReflector.Reflect()

  at System.Web.Services.Description.ServiceDescriptionReflector.ReflectInterna

l(ProtocolReflector[] reflectors)

  at System.Web.Services.Description.ServiceDescriptionReflector.Reflect(Type t

ype, String url)

  at System.Web.Services.Protocols.DiscoveryServerType..ctor(Type type, String

uri)

  at System.Web.Services.Protocols.DiscoveryServerProtocol.Initialize()

  at System.Web.Services.Protocols.ServerProtocolFactory.Create(Type type, Http

Context context, HttpRequest request, HttpResponse response, Boolean& abortProce

ssing)

---------------------------

When I comment above function out, web service compiles fine. Any ideas anybody?  Also, this works fine with other dictionaries.

# SharePoint Conference 2008 - Day 4 &laquo; Steve Pietrek&#8217;s SharePoint Stuff

Pingback from  SharePoint Conference 2008 - Day 4 &laquo; Steve Pietrek&#8217;s SharePoint Stuff

# re: XML Serializable Generic Dictionary

Thursday, March 13, 2008 8:51 AM by Hoytster

It doesn't work for me. :(

I'm serializing to a file. The test output looks like:

<item><key><string>Key1</string></key><value><string>Value1</string></value></item>

<item><key><string>Key2</string></key><value><string>Value2</string></value></item>

<item><key><string>Key3</string></key><value><string>Value3</string></value></item>

except its not wrapped... it's all on one line.

Isn't there supposed to be a root element?

It fails on the first reader.ReadEndElement() in ReadXml(), with the exception:

"There are multiple root elements. Line 1, position 85."

That column (85) is where the second <item> tag starts.

I'm a noob, and there's no usage example. :(

I used:

XmlTextWriter writer = new XmlTextWriter(@"c:\tmp\test2.xml", System.Text.Encoding.Default);

and

XmlTextReader reader = new XmlTextReader(@"c:\tmp\test.xml");

Suggestions? I'm sure I'm just calling it wrong, since it's working for everyone else.

Thanks, Hoytster

# XML Serializable Generic Dictionary tipi | Turk Bili??im Teknolojileri Ve G??venlik Platformu | iyinet webmaster forumu 2008 seo yar????mas?? |

Pingback from  XML Serializable Generic Dictionary tipi | Turk Bili??im Teknolojileri Ve G??venlik Platformu | iyinet webmaster forumu 2008 seo yar????mas?? |

# re: XML Serializable Generic Dictionary

Thursday, April 03, 2008 11:29 AM by Greg Harris

Thanks so much. Works perfectly for what I need. Nice to see it still being found so easily years later!

# re: XML Serializable Generic Dictionary

Monday, May 05, 2008 4:38 PM by Darin

why is :

SerializableDictionary<string, string> _testdict;

       [System.Runtime.Serialization.DataMember]

       public SerializableDictionary<string, string> TestCollection

       {

           get { return _testdict; }

           set { _testdict = value; }

       }

resulting in:

[System.Xml.Serialization.XmlElementAttribute(Order=0)]

       public System.Data.DataSet TestCollection

       {

           get

           {

               return this.testCollectionField;

           }

           set

           {

               this.testCollectionField = value;

           }

       }

dont want a DATASET !!! what can i change !!! (other then that this is a great class)

# re: XML Serializable Generic Dictionary

Tuesday, May 06, 2008 6:22 AM by BrightSoul

It works wonders, thanks. It saved me a lot of time.

# re: XML Serializable Generic Dictionary

Tuesday, May 13, 2008 9:20 AM by jan wilmans

public ref class StringDict : public SerializableDictionary<System::String^, System::String^> {};

bool foobool = true;

StringDict ^ userData = gcnew StringDict();

userData["foo"] = foobool ? "1":"0";

userData["bar"] = "theone";

userData["fobia"] = "really";

XmlTextWriter ^ xmlWriter = gcnew XmlTextWriter("dict.xml", nullptr);

xmlWriter->Formatting = Formatting::Indented;

xmlWriter->WriteStartElement("addondata");

userData->WriteXml(xmlWriter);

xmlWriter->WriteEndElement();

xmlWriter->Close();

XmlTextReader ^ xmlReader = gcnew XmlTextReader("dict.xml");

StringDict ^ sd = gcnew StringDict();

xmlReader->ReadStartElement("addondata");

sd->ReadXml(xmlReader);

xmlReader->Close();

# re: XML Serializable Generic Dictionary

Tuesday, May 27, 2008 1:39 PM by Craig

I am having the same problem mentioned in several other posts on this article, that being, the class deserializes as a DataSet for some unknown reason.

Strangely enough I have used it with success elsewhere. Am I missing something small but fundamental?

# re: XML Serializable Generic Dictionary

Wednesday, May 28, 2008 10:55 AM by Surya

Using in code

-------------------------

string txt;

       using (StringWriter string_writer = new StringWriter())

       {

           xml_serializer.Serialize(string_writer, sd);

           txt = string_writer.ToString();

           string_writer.Close();

       }

                      SerializableDictionary<string, string> _sd = new SerializableDictionary<string, string>();

               using (StringReader string_reader = new StringReader(txt))

               {

                   _sd = (SerializableDictionary<string, string>)xml_serializer.Deserialize(string_reader);

               }

# re: XML Serializable Generic Dictionary

Friday, May 30, 2008 8:29 AM by Graham

Thanks... works a treat.

# re: XML Serializable Generic Dictionary

Wednesday, June 04, 2008 9:44 AM by Amer Gerzic

Thanks for the tip!

# re: XML Serializable Generic Dictionary

Wednesday, June 11, 2008 3:30 PM by Samuel

Really great work... thanks for sharing it!

# re: XML Serializable Generic Dictionary

Tuesday, June 17, 2008 1:17 PM by Charlie

This is not writing the dictionary, this is writing the contents of the dictionary. It should encapsulate the content in another element.

# XmlConverter - serialize/deserialize an object to Xml &laquo; GAnton&#8217;s Weblog

Pingback from  XmlConverter - serialize/deserialize an object to Xml &laquo; GAnton&#8217;s Weblog

# re: XML Serializable Generic Dictionary

Saturday, June 21, 2008 1:54 PM by Steve

You rock. Thank you, you saved me hours!

# re: XML Serializable Generic Dictionary

Thursday, July 24, 2008 10:28 AM by shahzad

Thanks , Saved my alot of time.

# Displaying large amount of data in DataGridView | devintelligence.com

Pingback from  Displaying large amount of data in DataGridView | devintelligence.com

# re: XML Serializable Generic Dictionary

Sunday, August 10, 2008 3:47 PM by Reino Boonstra

Absolutely great! Thanks for sharing, works great!

# re: XML Serializable Generic Dictionary

Tuesday, August 12, 2008 4:43 AM by Magnus

To be able to deserialize the dictionary with a binary formatter add constructor:

protected XmlDictionary(SerializationInfo info, StreamingContext context)

           : base(info, context)

       {

       }

# re: XML Serializable Generic Dictionary

Monday, August 18, 2008 9:14 AM by Developer

I am trying to call the Add method on the Dictionary from diffrent pages, i have to create a new instance of Dictionary, and my data from first page is not reflected in the next page.

Can you help?

# Serializable dictionary &laquo; Billy club Weblog

Sunday, August 31, 2008 7:32 AM by Serializable dictionary « Billy club Weblog

Pingback from  Serializable dictionary &laquo; Billy club Weblog

# XmlConverter - serialize/deserialize an object to/from Xml &raquo; Anton Gochev&#8217;s Weblog

Pingback from  XmlConverter - serialize/deserialize an object to/from Xml &raquo; Anton Gochev&#8217;s Weblog

# re: XML Serializable Generic Dictionary

Sunday, October 26, 2008 4:39 AM by maha

serializable dictinary is compiled as a dataset//how can I fix this?

# re: XML Serializable Generic Dictionary

Friday, November 14, 2008 8:26 PM by iHenry

Hi there,

Serializable dictinary is compiled as a dataset, is there a way to fix this???

I'm using a work arround returning a String :S! because is better for me than replace it manually each time that I updated my references

To send:

StringWriter Output = new StringWriter(new StringBuilder());

           XmlSerializer xs = new XmlSerializer(typeof(SerializableDictionary<string, string>));

           xs.Serialize(Output, _SettingsFromWeb.AppSettings);            

           return Output.ToString();

And to receive

after get the string (settingString) from the web method

  XmlSerializer s = new XmlSerializer(typeof(SerializableDictionary<string, string>));

           SerializableDictionary<string, string> setting =

               (SerializableDictionary<string, string>)s.Deserialize(new StringReader(settingString));

Any suggestion??

# re: XML Serializable Generic Dictionary

Monday, December 15, 2008 9:51 PM by Krzysztof Sledziewski

Many thanks for that!

# re: XML Serializable Generic Dictionary

Tuesday, December 23, 2008 2:16 PM by Pavan

i am trying to use this code but getting an error.. i am using the Dictionary<string,Myobject>  While serializing this object i am getting this error .

"Use the XmlInclude or SoapInclude attribute to specify types that are not known statically."

class people

{

   extendedproperties SerializableDictionary<string,MyObject>;

}

in this case i am adding an extended property of type 'Person' in to people. now i try to serialize the People object. My person object is serializable object. can you tell me where i am going wrong. Thanks in advance.

# re: XML Serializable Generic Dictionary

Monday, December 29, 2008 9:25 PM by Seanba

Thank you for sharing, Paul.  It works great for my Settings.settings and Settings.Designer.cs files.

It's a shame C# 2.0 didn't come stock with a serializable dictionary.

# re: XML Serializable Generic Dictionary

Many thanks for that!

# re: XML Serializable Generic Dictionary

Monday, January 12, 2009 3:55 AM by Amol

Thank you for your efforts Paul.

# re: XML Serializable Generic Dictionary

Monday, January 19, 2009 6:20 AM by Steve

Many thanks Paul, I was going mad debugging until I found this - and it seems to work OK if you change it to a SortedDictionary too.

# re: XML Serializable Generic Dictionary

Tuesday, February 03, 2009 3:40 AM by Dean

Thanks! I appreciate you sharing your expertise.

# re: XML Serializable Generic Dictionary

Sunday, February 08, 2009 5:41 AM by Mark

Good job Paul! Kudos!

# Diccionario serializable &laquo; Tempus Fugit

Tuesday, February 24, 2009 5:43 AM by Diccionario serializable « Tempus Fugit

Pingback from  Diccionario serializable &laquo; Tempus Fugit

# re: XML Serializable Generic Dictionary

Wednesday, March 04, 2009 8:03 AM by Alpha

Thanks! You sure saved me lots of times and headaches.

# Ana??l Abrantes : mon blog technique .net &raquo; S??rialiser un dictionnaire g??n??rique (Dictionary)

Pingback from  Ana??l Abrantes : mon blog technique .net  &raquo; S??rialiser un dictionnaire g??n??rique (Dictionary)

# re: XML Serializable Generic Dictionary

Friday, March 13, 2009 10:11 AM by ...

Sehr wertvolle Informationen! Empfehlen!

# re: XML Serializable Generic Dictionary

Saturday, March 14, 2009 3:52 PM by ...

Sehr gute Seite. Ich habe es zu den Favoriten.

# re: XML Serializable Generic Dictionary

Monday, March 16, 2009 4:36 PM by Kent

Wouldn't the below be a little more efficient

if (reader.IsEmptyElement)

       return;

reader.Read();

instead of

bool wasEmpty = reader.IsEmptyElement;

reader.Read();

if (wasEmpty)

       return;

# re: XML Serializable Generic Dictionary

Friday, April 10, 2009 11:25 AM by nick_ropasl

# Wie sichert man am besten Programmdaten und Arrays - Seite 2 | hilpers

Pingback from  Wie sichert man am besten Programmdaten und Arrays - Seite 2 | hilpers

# re: XML Serializable Generic Dictionary

Sunday, April 12, 2009 9:17 PM by Nay

Thanks! very useful

# re: XML Serializable Generic Dictionary

Tuesday, April 21, 2009 11:48 AM by midas

Thanks! very useful.

# re: XML Serializable Generic Dictionary

Wednesday, May 13, 2009 5:39 AM by Thomas U.

Hi,

i encountered the same problem as Pavan, when trying to serialize "own user- objects" i also got this error:

InvalidOperationException:

"Use the XmlInclude or SoapInclude attribute to specify types that are not known statically."

This error occurs because the serializer does not know the Types to be Serialized/Deserialized, but you can feed the

serializer with the missing types:

   [XmlRoot("dictionary")]

   public class SerializableDictionary<TKey, TValue> : Dictionary<TKey, TValue>, IXmlSerializable

   {

public static List<Type> AdditionalTypes = new List<Type>();

...

XmlSerializer keySerializer = new XmlSerializer(typeof(TKey));

           XmlSerializer valueSerializer = new XmlSerializer(typeof(TValue),AdditionalTypes.ToArray());

...

XmlSerializer keySerializer = new XmlSerializer(typeof(TKey));

           XmlSerializer valueSerializer = new XmlSerializer(typeof(TValue),AdditionalTypes.ToArray());

...

In this snippet I included a static list of the Types to be Serialized. The list has to be initialized with the "additional" Types that are but as "Values" in the Dictionary.

This solution work pretty well for me,

please let me know what you think about it.

Greetings

Thomas

# re: XML Serializable Generic Dictionary

Tuesday, May 19, 2009 8:56 PM by Jeff

Spectacular sir!

You are a gentleman and a scholar!

You give blogs a good name!

# re: XML Serializable Generic Dictionary

Thursday, June 04, 2009 7:17 AM by islami sohbet

Pingback from  Wie sichert man am besten Programmdaten und Arrays - Seite 2 | hilpers

# re: XML Serializable Generic Dictionary

Tuesday, June 09, 2009 11:18 PM by Dustin

Has anyone been able to get past the weird problem of the client seeing it as a dataset?

# re: XML Serializable Generic Dictionary

Thursday, June 11, 2009 10:55 AM by Djohnnie

The client sees it as a dataset because the GetSchema method returns null. Null is the default proxy generator representation of a dataset.

Is there any way to return a valid schema here?

# re: XML Serializable Generic Dictionary

Wednesday, June 17, 2009 10:56 AM by Mike

I'm trying to set a property to this type in my Settings.settings file, but when I browse for types, my class is not listed. If I type in the namespace/class exactly, Visual Studio tells me it is not defined. However, if I manually edit the Settings.Designer.cs file with the SerializableDictonary type (instead of the type declared in the Settings.settings file), everything works like a charm. My problem is that any changes to the Settings.settings file re-generate the Settings.Designer.cs code, destroying my changes. Does anyone have any idea why this type is not showing up in the type browser in the Settings.settings file?

Thanks,

Mike

# re: XML Serializable Generic Dictionary

Saturday, July 11, 2009 4:41 AM by venice

hi there.

this is a nice solution for serializing Dictionaries. thanks. saved me lots of time.

# re: XML Serializable Generic Dictionary

Thursday, August 20, 2009 4:34 PM by Curt

Paul,

Thanks so much for creating and posting this!  I ran into the serialization problem and immediately had visions of doing it by hand.  blah.  Your solution worked the first time!

# re: XML Serializable Generic Dictionary

Saturday, September 12, 2009 4:19 PM by Richard

I'm not sure why, but when using this to serialize to a string and working only with the XML fragment produced by this (i.e. a fragment of just the serialized dictionary, so <item> was the top-level element), I had to add two reader.EOF checks in ReadXml to avoid encountering the end of the file, like so:

   public void ReadXml(System.Xml.XmlReader reader)

   {

       XmlSerializer keySerializer = new XmlSerializer(typeof(TKey));

       XmlSerializer valueSerializer = new XmlSerializer(typeof(TValue));

       bool wasEmpty = reader.IsEmptyElement;

       reader.Read();

       if (wasEmpty)

           return;

       while (reader.NodeType != System.Xml.XmlNodeType.EndElement && !reader.EOF)

       {

           reader.ReadStartElement("item");

           reader.ReadStartElement("key");

           TKey key = (TKey)keySerializer.Deserialize(reader);

           reader.ReadEndElement();

           reader.ReadStartElement("value");

           TValue value = (TValue)valueSerializer.Deserialize(reader);

           reader.ReadEndElement();

           this.Add(key, value);

           reader.ReadEndElement();

           reader.MoveToContent();

       }

       if (!reader.EOF)

           reader.ReadEndElement();

   }

# re: XML Serializable Generic Dictionary

Wednesday, September 16, 2009 9:01 AM by Amit J R

Hi ,

I have used this class in my web service and application. When i am calling my webservice using the following code

FacebookServiceSoapClient client = new FacebookServiceSoapClient("localhost/FacebookService.asmx");

           client.GenerateSignatureCompleted += ((sender, resultArg) =>

           {

               parameterList.Add("sig", resultArg.Result);

               ((FacebookServiceSoapClient)sender).CloseAsync();

               doThis(BuildQuery(parameterList));

           });

           client.GenerateSignatureAsync(parameterList);

The last line of code "  client.GenerateSignatureAsync(parameterList);

" gives me an error "cannot convert from  'SilverlightFacebook.SerializableDictionary<string,string>' to 'SilverlightFacebook.FacebookService.ArrayOfXElement'"

In the WebService i have the following method

[WebMethod]

       public string GenerateSignature(SerializableDictionary<string, string> parameters)

       {}

Please can someone help me with this

# re: XML Serializable Generic Dictionary

Monday, September 28, 2009 5:39 PM by MIke K.

Works great. Thanks!

# Using Dictionary Object Params In Your Web Service

Monday, October 12, 2009 10:13 AM by Kevin Grohoske

Using Dictionary Object Params In Your Web Service

# Using Dictionary Object Params In Your Web Service

Monday, October 12, 2009 10:19 AM by Kevin Grohoske

Using Dictionary Object Params In Your Web Service

# re: XML Serializable Generic Dictionary

Tuesday, October 20, 2009 6:28 PM by Karim

Thanks a lot it works fine for me

# re: XML Serializable Generic Dictionary

Monday, November 02, 2009 6:36 AM by Sundip

HI pwelter34,

Great snippet.

Have a problem though. I have tried to create an SerializableDictionary inside another SerializableDictionary.

       private SerializableDictionary<EnumType1, SerializableDictionary<EnumType2, ObjectType>> _matrix;

This seems to fail. Please help.

Sundip

# re: XML Serializable Generic Dictionary

Tuesday, November 03, 2009 6:09 AM by johannes

In order to work for the latest framework it is necesairy to have a root element.

in the WriteXML add a line

+49 writer.WriteStartElement( "dictionary" );

+70 writer.WriteEndElement( );

in the ReadXML add lines

+27 reader.ReadStartElement( "dictionary" );

+45 reader.ReadEndElement( );

# re: XML Serializable Generic Dictionary

Sunday, November 08, 2009 1:10 PM by ALTİNDAMLALAR.COM

Ancak bir sorun var. Başka bir SerializableDictionary içinde bir SerializableDictionary oluşturmak için çalıştık.

Özel SerializableDictionary <EnumType1, SerializableDictionary <EnumType2,

# Culture-aware business objects

Thursday, November 12, 2009 12:44 PM by WIS3GUY.NET

Culture-aware business objects

# re: XML Serializable Generic Dictionary

Saturday, November 28, 2009 6:13 AM by DJ_AGLAMAZ@hotmail.com

çok güzel olmuş tbler

http://www.altindamlalar.com

# re: XML Serializable Generic Dictionary

Friday, December 11, 2009 7:33 PM by Suenthen

your site is loading fast

# re: XML Serializable Generic Dictionary

Wednesday, December 23, 2009 6:43 AM by Irish Peeps

 any more posts coming ?

# re: XML Serializable Generic Dictionary

Monday, January 04, 2010 2:39 AM by islami sohbet

thanks a very nice

# re: XML Serializable Generic Dictionary

Wednesday, January 06, 2010 6:03 AM by BIZBIZ20032003

# re: XML Serializable Generic Dictionary

Thursday, January 07, 2010 10:31 AM by blacxcom

Very nice script in XML. I will try it.

# re: XML Serializable Generic Dictionary

Thursday, January 07, 2010 12:33 PM by islami sohbet

Thanks harita xml :)

# re: XML Serializable Generic Dictionary

Sunday, January 17, 2010 11:41 AM by MikeP

I,m getting an error in ReadXML at"

this.Add(key, value);

"An item with the same key has already been added"

This is in the first iteration.

"this" looks like its a completely filled (correctly)dictionary at this point in the debugging .

# re: XML Serializable Generic Dictionary

Damn, that sound's so easy if you think about it.

# re: XML Serializable Generic Dictionary

Saturday, February 06, 2010 7:35 AM by zalim

Leave a Comment

(required) 
(required) 
(optional)
(required)