Having empty Dictionary<int, string>
how to fill it with keys and values from XML like
<items>
<item id='int_goes_here' value='string_goes_here'/>
</items>
and serialize it back into XML not using XElement?
Having empty Dictionary<int, string>
how to fill it with keys and values from XML like
<items>
<item id='int_goes_here' value='string_goes_here'/>
</items>
and serialize it back into XML not using XElement?
Write a class A, that contains of an array of class B. Class B should have an id property and a value property. Deserialize the xml to class A. Convert the array in A to the wanted dictionary.
To serialize the dictionary convert it to an instance of class A, and serialize...
There is an easy way with Sharpeserializer (open source) :
http://www.sharpserializer.com/
It can directly serialize/de-serialize dictionary.
There is no need to mark your object with any attribute, nor do you have to give the object type in the Serialize method (See here ).
To install via nuget :
Install-package sharpserializer
Then it is very simple :
Hello World (from the official website):
I use serializable classes for the WCF communication between different modules. Below is an example of serializable class which serves as DataContract as well. My approach is to use the power of LINQ to convert the Dictionary into out-of-the-box serializable List<> of KeyValuePair<>:
The usage is straightforward - I assign a dictionary to my data object's dictionary field - DictionaryX. The serialization is supported inside the SerializableClassX by conversion of the assigned dictionary into the serializable List<> of KeyValuePair<>: