我正在尝试编写可以存储在app.config或web.config文件中的通用字典。按照this answer中的示例,我写了一个字典,做了一些更改:
System.Xml.Linq
API [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>
答案 0 :(得分: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;
}
}
}