Click here to Skip to main content
13,148,427 members (60,621 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


Posted 13 Jan 2012

XmlSerializer doesn't work with Dictionaries. Oh, and it has problems with KeyValuePairs too.

, 13 Jan 2012
Rate this:
Please Sign up or sign in to vote.
Why not use the DataContractSerializer instead?Dictionary dict = new Dictionary();dict.Add("myKey", "myValue");MemoryStream memoryStream = new MemoryStream();DataContractSerializer serializer = new...
Why not use the DataContractSerializer instead?

Dictionary<string, string> dict = new Dictionary<string,string>();
dict.Add("myKey", "myValue");
MemoryStream memoryStream = new MemoryStream();
DataContractSerializer serializer = new DataContractSerializer(dict.GetType());
serializer.WriteObject(memoryStream, dict);
memoryStream.Position = 0;
StreamReader reader = new StreamReader(memoryStream);
string str = reader.ReadToEnd();

str will look like
<ArrayOfKeyValueOfstringstring xmlns="" xmlns:i="">


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Web Developer
Ireland Ireland
This member doesn't quite have enough reputation to be able to display their biography and homepage.

You may also be interested in...


Comments and Discussions

GeneralReason for my vote of 5 very nice Pin
HoyaSaxa9313-Jan-12 7:29
memberHoyaSaxa9313-Jan-12 7:29 
GeneralI said there was probably a much, much nicer way to do it! :... Pin
OriginalGriff13-Jan-12 2:08
mvpOriginalGriff13-Jan-12 2:08 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170924.2 | Last Updated 13 Jan 2012
Article Copyright 2012 by vfinn
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid