zoukankan      html  css  js  c++  java
  • C# Dictionary的xml序列化

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.IO;
     6 using System.Xml;
     7 using System.Xml.Schema;
     8 using System.Xml.Serialization;
     9 namespace SerialDict
    10 {
    11     [Serializable]
    12     public class SerialDictionary<TKey, TValue> : Dictionary<TKey, TValue>, IXmlSerializable
    13     {
    14         private string name;
    15         public SerialDictionary()
    16         {
    17             this.name = "SerialDictionary";
    18         }
    19         public SerialDictionary(string name)
    20         {
    21             this.name = name;
    22         }
    23         public void WriteXml(XmlWriter write)       // Serializer
    24         {
    25             XmlSerializer KeySerializer = new XmlSerializer(typeof(TKey));
    26             XmlSerializer ValueSerializer = new XmlSerializer(typeof(TValue));
    27 
    28             
    29             write.WriteAttributeString("name", name);
    30             
    31             write.WriteStartElement(name);
    32             foreach (KeyValuePair<TKey, TValue> kv in this)
    33             {
    34                 write.WriteStartElement("element");
    35                 write.WriteStartElement("key");
    36                 KeySerializer.Serialize(write, kv.Key);
    37                 write.WriteEndElement();
    38                 write.WriteStartElement("value");
    39                 ValueSerializer.Serialize(write, kv.Value);
    40                 write.WriteEndElement();
    41                 write.WriteEndElement();
    42             }
    43             write.WriteEndElement();
    44         }
    45         public void ReadXml(XmlReader reader)       // Deserializer
    46         {
    47             reader.Read();
    48             XmlSerializer KeySerializer = new XmlSerializer(typeof(TKey));
    49             XmlSerializer ValueSerializer = new XmlSerializer(typeof(TValue));
    50         
    51             name = reader.Name;
    52             reader.ReadStartElement(name);
    53             while (reader.NodeType != XmlNodeType.EndElement)
    54             {
    55                 reader.ReadStartElement("element");
    56                 reader.ReadStartElement("key");
    57                 TKey tk = (TKey)KeySerializer.Deserialize(reader);
    58                 reader.ReadEndElement();
    59                 reader.ReadStartElement("value");
    60                 TValue vl = (TValue)ValueSerializer.Deserialize(reader);
    61                 reader.ReadEndElement();
    62                 reader.ReadEndElement();
    63                 
    64                 this.Add(tk, vl);
    65                 reader.MoveToContent();
    66             }
    67             reader.ReadEndElement();
    68             reader.ReadEndElement();
    69 
    70         }
    71         public XmlSchema GetSchema()
    72         {
    73             return null;
    74         }
    75     }
    76     class Program
    77     {
    78         static void Main(string[] args)
    79         {
    80             SerialDictionary<string, uint> sd = new SerialDictionary<string, uint>("modelNextIDs");
    81             sd.Add("xxxx", 10);
    82             sd.Add("xxxxx", 1);
    83             sd.Add("xxxxxx", 1);
    84             string fileName = "D:\s.xml";
    85             using (FileStream fileStream = new FileStream(fileName, FileMode.Create))
    86             {
    87                 XmlSerializer xmlFormatter = new XmlSerializer(sd.GetType());
    88                 xmlFormatter.Serialize(fileStream, sd);
    89             }
    90 
    91             SerialDictionary<string, uint> sd2 = new SerialDictionary<string, uint>("modelNextIDs");
    92             using (FileStream fileStream2 = new FileStream(fileName, FileMode.Open))
    93             {
    94                 XmlSerializer xmlFormatter = new XmlSerializer(sd2.GetType());
    95                 sd2 = xmlFormatter.Deserialize(fileStream2) as SerialDictionary<string, uint>;
    96             }
    97         }
    98     }
    99 }
  • 相关阅读:
    C# SQL 语句 更新树表的全路径
    递归树 C#
    javascript 在没有找到何时的脚本管理框架前, 使用如下方式在页面上使用脚本
    JS keycode keydown keypress 事件
    C# asp.net 解压缩
    弹出新窗体出现的含有[object]空白页面问题的解决
    include define【11/12/15】
    HDOJ 2191 珍惜现在,感恩生活 【动态规划 多重背包】
    HDOJ 1114 Piggy-Bank 【动态规划 完全背包】
    HDOJ 2602 Bone Collector 【动态规划 01背包】
  • 原文地址:https://www.cnblogs.com/gomen/p/3509768.html
Copyright © 2011-2022 走看看