2

I'm trying to write a general purpose dictionary that can be stored in an app.config or web.config file. Following the example in this answer, I've written a dictionary, with some changes:

  1. I used the more modern System.Xml.Linq API
  2. I used a Hashtable, which allows me to store a value of any type, as long as it's serializable, and not just strings.
    [SettingsSerializeAs(SettingsSerializeAs.Xml)]
    public class XmlSerializableDictionary : Hashtable, IXmlSerializable
    {

        #region Constructors

        /// <summary>
        /// Initializes a new instance of <see cref="XmlSerializableDictionary"/>.
        /// </summary>
        public XmlSerializableDictionary()
        {

        }

        #endregion

        #region IXmlSerializable Members

        /// <inheritdoc />
        public XmlSchema GetSchema()
        {
            return null;
        }

        /// <inheritdoc />
        public void ReadXml(XmlReader reader)
        {
            XDocument xdoc = XDocument.Load(reader);
            foreach (XElement entry in xdoc.Elements())
            {
                XElement key = entry.Element(nameof(DictionaryEntry.Key))
                    ?? throw new FormatException($"{nameof(DictionaryEntry.Key)} is missing in entry");
                XElement valueElement = entry.Element(nameof(DictionaryEntry.Value))
                                 ?? throw new FormatException($"{nameof(DictionaryEntry.Value)} element is missing in entry");
                XAttribute valueTypeName = valueElement?.Attribute("type") ?? throw new FormatException($"type attribute is missing in {nameof(DictionaryEntry.Value)} element");
                if (valueTypeName.Value == "null")
                {
                    this[key.Value] = null;
                }
                else
                {
                    Type valueType = Type.GetType(valueTypeName.Value);
                    XmlSerializer xmlSerializer = new XmlSerializer(valueType ?? throw new ArgumentException("type attribute value is not a valid type name."));
                    Object value = xmlSerializer.Deserialize(valueElement.CreateReader());
                    this[key.Value] = value;
                }
            }
        }

        /// <inheritdoc />
        public void WriteXml(XmlWriter writer)
        {
            XDocument xdoc = new XDocument();
            xdoc.Add(new XElement(nameof(XmlSerializableDictionary)));
            foreach (DictionaryEntry entry in this)
            {
                Type valueType = entry.Value?.GetType();
                String typeName = valueType == null ? "null" : $"{valueType.FullName}, {valueType.Assembly.GetName().Name}";
                Object value = null;
                if (valueType != null)
                {
                    XmlSerializer xmlSerializer = new XmlSerializer(valueType);
                    using (MemoryStream stream = new MemoryStream())
                    {
                        xmlSerializer.Serialize(stream, entry.Value);
                        using (StreamReader reader = new StreamReader(stream))
                        {
                            stream.Seek(0, SeekOrigin.Begin);
                            String xmlString = reader.ReadToEnd();
                            XElement valueXml = XElement.Load(xmlString);
                            value = valueXml;
                        }
                    }
                }
                XElement entryElement = new XElement(nameof(DictionaryEntry),
                    new XElement(nameof(DictionaryEntry.Key),
                        new XAttribute("type", typeName),
                        entry.Key.ToString()),
                    new XElement(nameof(DictionaryEntry.Value), value));
                xdoc.Root?.Add(entryElement);
            }
        }

        #endregion

        /// <inheritdoc />
        public override void Add(object key, object value)
        {
            if (!(key is String))
            {
                throw new NotSupportedException($"{nameof(key)} must be a string.");
            }
            base.Add(key, value);
        }
    }

The expected XML schema should be

<XmlSerializableDictionary>
    <DictionaryEntry>
        <Key>Key1</Key>
        <Value type="System.Int32"> <!-- example type-->
           Value1
        </Value>
    </DictionaryEntry>
    <!--other entries here -->
</XmlSerializableDictionary>

However, after adding a setting of this type in the settings designer, and a sample value, the setting is not written to the app.config file of the class library where this is implemented. Is there something I'm missing?

Update I updated the above XML as follows, but it still doesn't work:

<?xml version="1.0" encoding="utf-16"?>
<XmlSerializableDictionary xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <DictionaryEntry>
        <Key>Key1</Key>
        <Value type="System.Int32">42</Value>
    </DictionaryEntry>
</XmlSerializableDictionary>
Tsahi Asher
  • 1,767
  • 15
  • 28
  • Good call. I couldn't event get a string array to work, even though there is a type for it. – TaW Feb 13 '19 at 13:36
  • @TaW StringCollection works well for that. – Tsahi Asher Feb 13 '19 at 13:38
  • After hours of frustrating work I gave up on that and resorted to stuffing the string array into one string. Duh. I'd like to see an example that actually works. All I found were (often conflicting) posts that show all sorts of tricks (!) one should use. None worked here :-( – TaW Feb 13 '19 at 13:40

1 Answers1

0

Try to pass your stream to XmlReader like

if (valueType != null)
{
    XmlSerializer xmlSerializer = new XmlSerializer(valueType);
    using (MemoryStream stream = new MemoryStream())
    {
        xmlSerializer.Serialize(stream, entry.Value);
        stream.Position = 0;

        using (XmlReader reader = XmlReader.Create(stream))
        {
            XElement valueXml = XElement.Load(reader);
            value = valueXml;
        }
    }
}
er-sho
  • 9,581
  • 2
  • 13
  • 26
  • still doesn't work. Also tried to use the type from another project. This time the type showed up in the "Browse..." option, but it still didn't work. – Tsahi Asher Feb 18 '19 at 15:49