如何序列化和反序列化字典值

时间:2017-10-14 18:16:40

标签: c# .net

我如何将字典值(仅限值)序列化和反序列化为XML? 我正在制作一个包含大量词典的程序,而且我不可能将所有现有的词典更改为新的" SerializableDictionary"所以我正在考虑制作自己的XmlSerializer或类似的东西。

我想像这样得到一个干净的Xml:

<Runnable>
    <MyData>
        <Foo><Name>John</Name></Foo>
        <Foo2><Name>Joe</Name></Foo2>
    </MyData>
</Runnable>

class CustomXmlSerializer{
    public void Serialize(...){}
    public object Deserialize(...){}
}
class Foo{
    public int Name{get;set;}
}
class Foo2:Foo{
    public int Name{get;set;}
}
class Runnable{
    private Dictionary MyData = new Dictionary<int,Foo>;
    public Runnable(){   
    public void Save(){
        CustomXmlSerializer worker = new CustomXmlSerializer();
        worker.Serialize(MyData,"Data.xml");
    }
    public void Load(){
        CustomXmlSerializer worker = new CustomXmlSerializer();
        MyData = worker.Deserialize("Data.xml")
    }
}

0 个答案:

没有答案