using System.Collections.Generic;
using System.Xml.Serialization;
using Microsoft.VisualStudio.Services.Common;
public interface IMyInterface { }
public class SerializableDictWithGuid<T1, T2> : SerializableDictionary<T1, T2>, IMyInterface, IXmlSerializable
const string idXml001 = "idXml001";
public string Guid { get; set; } = string.Empty;
public new void ReadXml(XmlReader reader)
Guid = reader.GetAttribute(idXml001);
public new void WriteXml(XmlWriter writer)
writer.WriteAttributeString(idXml001, Guid);
public static void Test()
var type = typeof(SerializableDictWithGuid<string, string>);
Console.WriteLine("interfaces implemented by {0}", type);
foreach (var i in type.GetInterfaces())
Console.WriteLine(" " + i);
var methods = type.GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy | BindingFlags.Instance);
foreach (var method in methods.Where(m => m.Name.Contains("xml", StringComparison.OrdinalIgnoreCase)))
Console.WriteLine($"method name={method}, method.IsVirtual={method.IsVirtual}, declaring type = {method.DeclaringType}");
public static void Test(string guid)
var dictionary = new SerializableDictWithGuid<string, string>
var xml = dictionary.GetXml();
var dict2 = xml.LoadFromXml<SerializableDictWithGuid<string, string>>();
Assert.AreEqual(dictionary.Guid, dict2.Guid);
var xml2 = dict2.GetXml();
Assert.That(XNode.DeepEquals(XElement.Parse(xml), XElement.Parse(xml2)));
Assert.AreEqual(dictionary.Count, dict2.Count);
Assert.AreEqual(System.Text.Json.JsonSerializer.Serialize(dictionary), System.Text.Json.JsonSerializer.Serialize(dict2));
public static partial class XmlSerializationExtensions
public static T LoadFromXml<T>(this string xmlString, XmlSerializer serial = null)
serial = serial ?? new XmlSerializer(typeof(T));
using (var reader = new StringReader(xmlString))
return (T)serial.Deserialize(reader);
public static string GetXml<T>(this T obj, bool omitStandardNamespaces)
return obj.GetXml(null, omitStandardNamespaces);
public static string GetXml<T>(this T obj, XmlSerializer serializer = null, bool omitStandardNamespaces = false)
XmlSerializerNamespaces ns = null;
if (omitStandardNamespaces)
ns = new XmlSerializerNamespaces();
using (var textWriter = new StringWriter())
var settings = new XmlWriterSettings() { Indent = true };
using (var xmlWriter = XmlWriter.Create(textWriter, settings))
(serializer ?? new XmlSerializer(obj.GetType())).Serialize(xmlWriter, obj, ns);
return textWriter.ToString();
public static string GetOuterXml(this XmlNode node, bool indent = true)
using (var textWriter = new StringWriter())
var settings = new XmlWriterSettings
OmitXmlDeclaration = true,
using (var xmlWriter = XmlWriter.Create(textWriter, settings))
return textWriter.ToString();
public static void Main()
Console.WriteLine("Environment version: {0} ({1}, {2})", System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription , Environment.Version, Environment.OSVersion);
Console.WriteLine("Failed with unhandled exception: ");