如何在设置中存储字典

问题描述 投票:1回答:1

我正在尝试编写一个通用字典,可以存储在app.config或web.config文件中。按照this answer中的例子,我写了一本字典,但有一些变化:

  1. 我使用了更现代的System.Xml.Linq API
  2. 我使用了一个Hashtable,它允许我存储任何类型的值,只要它是可序列化的,而不仅仅是字符串。
    [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);
        }
    }

应该是预期的XML模式

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

但是,在设置设计器中添加此类型的设置和样本值后,该设置不会写入实现此类的类库的app.config文件。有什么我想念的吗?

更新我更新了上面的XML,但它仍然不起作用:

<?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>
c# xml app-config
1个回答
0
投票

尝试将你的stream传递给XmlReader

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;
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.