Click here to Skip to main content
11,631,992 members (72,647 online)
Click here to Skip to main content

Tagged as

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

, 13 Jan 2012 CPOL 5.7K
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="http://schemas.microsoft.com/2003/10/Serialization/Arrays" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<KeyValueOfstringstring>
<Key>myKey</Key>
<Value>myValue</Value>
</KeyValueOfstringstring>
</ArrayOfKeyValueOfstringstring>

License

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

Share

About the Author

vfinn
Web Developer
Ireland Ireland
No Biography provided

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    Rant Rant    Admin Admin   

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

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