Skip to content

Instantly share code, notes, and snippets.

@steelywing
Last active October 9, 2024 17:06
Show Gist options
  • Select an option

  • Save steelywing/c08ac7563ad1a918db84ffe406c397a9 to your computer and use it in GitHub Desktop.

Select an option

Save steelywing/c08ac7563ad1a918db84ffe406c397a9 to your computer and use it in GitHub Desktop.
C# SerializableDictionary
using System;
using System.Collections.Generic;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;
/// <summary>
/// Base on https://weblogs.asp.net/pwelter34/444961
/// </summary>
/// <typeparam name="TKey"></typeparam>
/// <typeparam name="TValue"></typeparam>
[XmlRoot("dictionary")]
public class SerializableDictionary<TKey, TValue> : Dictionary<TKey, TValue>, IXmlSerializable
{
// XmlSerializer.Deserialize() will create a new Object, and then call ReadXml()
// So cannot use instance field, use class field.
public static string itemTag = "item";
public static string keyTag = "key";
public static string valueTag = "value";
public XmlSchema GetSchema()
{
return null;
}
public void ReadXml(XmlReader reader)
{
if (reader.IsEmptyElement)
return;
var keySerializer = new XmlSerializer(typeof(TKey));
var valueSerializer = new XmlSerializer(typeof(TValue));
reader.ReadStartElement();
// IsStartElement() will call MoveToContent()
// reader.MoveToContent();
while (reader.IsStartElement(itemTag))
{
reader.ReadStartElement(itemTag);
reader.ReadStartElement(keyTag);
TKey key = (TKey)keySerializer.Deserialize(reader);
reader.ReadEndElement();
reader.ReadStartElement(valueTag);
TValue value = (TValue)valueSerializer.Deserialize(reader);
reader.ReadEndElement();
reader.ReadEndElement();
this.Add(key, value);
// IsStartElement() will call MoveToContent()
// reader.MoveToContent();
}
reader.ReadEndElement();
}
public void WriteXml(XmlWriter writer)
{
var keySerializer = new XmlSerializer(typeof(TKey));
var valueSerializer = new XmlSerializer(typeof(TValue));
foreach (var kvp in this)
{
writer.WriteStartElement(itemTag);
writer.WriteStartElement(keyTag);
keySerializer.Serialize(writer, kvp.Key);
writer.WriteEndElement();
writer.WriteStartElement(valueTag);
valueSerializer.Serialize(writer, kvp.Value);
writer.WriteEndElement();
writer.WriteEndElement();
}
}
}
using System;
using System.IO;
using System.Xml.Serialization;
//[XmlRoot("AnimalDictionary")]
public class SDict : SerializableDictionary<string, string>
{
}
namespace Test
{
public class Program
{
static void Main(string[] args)
{
var dict = new SDict();
//SDict.itemTag = "AnimalCount";
//SDict.keyTag = "Type";
//SDict.valueTag = "Count";
dict.Add("Cat", "1");
dict.Add("Dog", "2");
foreach (var kvp in dict)
Console.WriteLine(kvp);
XmlSerializer serializer = new XmlSerializer(typeof(SDict));
using (var writer = new StreamWriter("dict.xml"))
{
serializer.Serialize(writer, dict);
}
Console.WriteLine("\nXML File:");
Console.WriteLine(File.ReadAllText("dict.xml"));
using (var reader = new StreamReader("dict.xml"))
{
dict = serializer.Deserialize(reader) as SDict;
}
Console.WriteLine("\nAfter Deserialize");
foreach (var kvp in dict)
Console.WriteLine(kvp);
Console.ReadLine();
}
}
}
@Issung
Copy link
Copy Markdown

Issung commented May 21, 2023

Thanks so much for this, dropped in and just worked perfectly for my need ๐ŸŽ‰

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment