Click here to Skip to main content
15,891,942 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
Dictionary<int, List<int>> Map_list = new Dictionary<int, List<int>>();


This is the code for convert Dictionary to XML document.

C#
XmlSerializer serialiser1 = new XmlSerializer(typeof(Dictionary<int, List<int>>))  
TextWriter Filestream1 = new StreamWriter(@"E:\dic.xml");
serialiser1.Serialize(Filestream1, Map_list);
Filestream1.Close();



But when debugging there is an exception thrown.

The type System.Collections.Generic.Dictionary`2[[System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.Collections.Generic.List`1[[System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]] is not supported because it implements IDictionary.


please any one correct my code !

Thanks in Advance!
Posted
Updated 10-Dec-15 3:04am
v2
Comments
F. Xaver 10-Dec-15 8:58am    
yeha.. knowing WHAT exception is thrown would be usefull ;)
Kasthuri Gunabalasingam 10-Dec-15 9:02am    
The type System.Collections.Generic.Dictionary`2[[System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.Collections.Generic.List`1[[System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]] is not supported because it implements IDictionary.
ridoy 10-Dec-15 9:28am    
check: http://www.codeproject.com/Questions/454074/From-Dictionary-to-XML

Here's the good news: yes, you can serialize a dictionary ... unless either the Dictionary Key or Value Types are some strange beast like a WinForm Control. WCF DataContract and DataMember tools are quite powerful; you can even serialize recursive structures.

Here's what the XML produced by serializing a Dictionary<int,List<int>> looks like:
XML
<serializetest xmlns="http://schemas.datacontract.org/2004/07/Dec_8_SDI_App" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><map_list xmlns:a="http://schemas.microsoft.com/2003/10/Serialization/Arrays"><a:keyvalueofintarrayofintty7ep6d1><a:key>0</a:key><a:value><a:int>1</a:int><a:int>2</a:int><a:int>3</a:int><a:int>4</a:int></a:value></a:keyvalueofintarrayofintty7ep6d1><a:keyvalueofintarrayofintty7ep6d1><a:key>1</a:key><a:value><a:int>5</a:int><a:int>6</a:int><a:int>7</a:int><a:int>8</a:int></a:value></a:keyvalueofintarrayofintty7ep6d1></map_list></serializetest>
Here's the executed code that wrote that Dictionary, and that reads it back in again:
C#
private void button2_Click(object sender, EventArgs e)
{
    string filePath = @"C:\Users\YourUserName\Desktop\sTest.xml";

    SerializeTest sTest = new SerializeTest();

    sTest.Map_list = new Dictionary<int, List<int>>
    {
        {0, new List<int> {1, 2, 3, 4}},
        {1, new List<int> {5, 6, 7, 8}}
    };

    SerializeTest.Serialize(filePath, sTest);

    SerializeTest ReadTest = SerializeTest.DeSerialize(filePath);

}
Here's the Class used to test serializing the Dictionary:
C#
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization;

namespace YourNameSpace
{
    [DataContract]
    public class SerializeTest
    {
        [DataMember]
        public Dictionary<int, List<int>> Map_list {set; get;}
    
        public SerializeTest()
        {
            Map_list = new Dictionary<int, List<int>>();
        }

        private static DataContractSerializer serializer;

        public static void Serialize(string filePath, SerializeTest sTest)
        {
            serializer = new DataContractSerializer(typeof(SerializeTest));

            using (var writer = new FileStream(filePath, FileMode.Create, FileAccess.Write))
            {
                serializer.WriteObject(writer, sTest);
            } 
        }
   
        public static SerializeTest DeSerialize(string filePath)
        {
            serializer = new DataContractSerializer(typeof(SerializeTest));

            SerializeTest serializeTest;

            using (var reader = new FileStream(filePath, FileMode.Open, FileAccess.Read))
            {
                serializeTest = serializer.ReadObject(reader) as SerializeTest;
            }

            return serializeTest;
        }
    }
}
 
Share this answer
 
v2
Comments
Kasthuri Gunabalasingam 10-Dec-15 13:45pm    
Thank you so much !.. :)It's worked for me as expected :) Thanks again !
Sergey Alexandrovich Kryukov 10-Dec-15 15:41pm    
Correct solution, 5ed. I removed mine as totally redundant.
—SA
[no name] 10-Dec-15 20:18pm    
Wow!
You can't serialise a dictionary. You'll need to implement your own serialisation or maybe use something like the link below that has a dictionary that can be serialised

http://stackoverflow.com/questions/495647/serialize-class-containing-dictionary-member[^]
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 10-Dec-15 11:44am    
I posted my solution and explained it, but Solution 3 clearly convinced that I was wrong. So, my solution, which perfectly works, is still totally redundant, so I removed it.
Your statement that a dictionary cannot be serialized is wrong, and I made the same mistake. It is not serializable relative to obsolete legacy serialization, but is well serialized using Data Contract, which I recommended in first place, but never checked up the false statement that dictionary is not serializable. I corrected my mistake and removed my solution, and recommend you doing the same.

Solution 3 is a complete solution.

—SA
BillWoodruff 10-Dec-15 13:08pm    
Well, sorry, you can serialize a Dictionary. See my solution, here.
F-ES Sitecore 11-Dec-15 3:26am    
I obviously meant it wasn't possible in the way he was doing. I then linked to a solution that detailed a technique that was identical to the one you posted. I'm just glad my advice was right, no need to apologise.

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900