将Dictionary转换为XML文件 [英] convert Dictionary to XML file

查看:156
本文介绍了将Dictionary转换为XML文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Dictionary<int, List<int>> Map_list = new Dictionary<int, List<int>>();





这是将Dictionary转换为XML文档的代码。





This is the code for convert Dictionary to XML document.

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!

推荐答案

这是个好消息:是的,你可以序列化一个字典 ...除非字典键或值类型是一些奇怪的野兽,如WinForm控件。 WCF DataContract和DataMember工具非常强大;你甚至可以序列化递归结构。



这是通过序列化Dictionary< int,List< int>>生成的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:
<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:

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:

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;
        }
    }
}


您无法序列化字典。您需要实现自己的序列化,或者使用下面的链接,其中包含可以序列化的字典



http://stackoverflow.com/questions/495647/serialize-class-containing-dictionary-member [ ^ ]
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[^]


这篇关于将Dictionary转换为XML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆