簡體   English   中英

序列化字典 <string, object> 包含一個列表 <string> ?

[英]Serialize a Dictionary<string, object> that contains a List<string>?

我找到了一個效果很好的序列化字典: http : //www.jankowskimichal.pl/en/2010/10/serializabledictionary/

但是,只要字典中的對象之一僅僅是字符串列表,我就會遇到異常。 .NET告訴我它無法序列化:

Serialize 'C:\bin\Debug\Settings\Default.xml' : System.InvalidOperationException: There was an error generating the XML document. ---> System.InvalidOperationException: There was an error generating the XML document. ---> System.InvalidOperationException: The type System.Collections.Generic.List`1[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]] may not be used in this context.
   at System.Xml.Serialization.XmlSerializationWriter.WriteTypedPrimitive(String name, String ns, Object o, Boolean xsiType)
   at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterObject.Write1_Object(String n, String ns, Object o, Boolean isNullable, Boolean needType)
   at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterObject.Write2_anyType(Object o)
   --- End of inner exception stack trace ---
   at System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter, Object o, XmlSerializerNamespaces namespaces, String encodingStyle, String id)
   at System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter, Object o)
   at ShadowBot.Classes.SerializableDictionary`2.WriteXml(XmlWriter writer) in c:\Classes\SerializableDictionary.cs:line 114
   at System.Xml.Serialization.XmlSerializationWriter.WriteSerializable(IXmlSerializable serializable, String name, String ns, Boolean isNullable, Boolean wrapped)
   at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterShadowSettings.Write2_ShadowSettings(String n, String ns, ShadowSettings o, Boolean isNullable, Boolean needType)
   at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterShadowSettings.Write3_ShadowSettings(Object o)
   --- End of inner exception stack trace ---
   at System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter, Object o, XmlSerializerNamespaces namespaces, String encodingStyle, String id)
   at System.Xml.Serialization.XmlSerializer.Serialize(Stream stream, Object o, XmlSerializerNamespaces namespaces)
   at System.Xml.Serialization.XmlSerializer.Serialize(Stream stream, Object o)
   at Classes.XmlSerializer.Serialize(String Path, Object Object) in c:\Classes\XmlSerializer.cs:line 29

這有可能嗎? 我想要這種功能,只是假設您可以嵌套這樣的對象。 如果無法做到這一點,是否有一種方法可以將字典寫入磁盤(不必是XML),然后重新加載字典,而無需為此編寫自定義包裝器? 我最初是一名Mac開發人員,這種類型的序列化非常簡單,所以也許我在.NET上缺少某些東西。

編輯:當嘗試奧德斯的例子時,我得到:

System.Runtime.Serialization.SerializationException: Type 'System.Collections.Generic.List`1[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]' with data contract name 'ArrayOfstring:http://schemas.microsoft.com/2003/10/Serialization/Arrays' is not expected. Consider using a DataContractResolver or add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to DataContractSerializer.

作為替代方案,使用System.Runtime.Serialization.DataContractSerializer可能會更好。 例:

Dictionary<string, List<string>> dict = new Dictionary<string, List<string>>();
dict.Add("test", new List<string>() { "t", "b" });

StringBuilder xmlString = new StringBuilder();
using (XmlWriter writer = XmlWriter.Create(xmlString))
{
    DataContractSerializer serializer = new DataContractSerializer(typeof(Dictionary<string, List<string>>));
    serializer.WriteObject(writer, dict);
}

這將產生:

<?xml version="1.0" encoding="utf-16"?><ArrayOfKeyValueOfstringArrayOfstringty7Ep6D1 xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/2003/10/Serialization/Arrays"><KeyValueOfstringArrayOfstringty7Ep6D1><Key>test</Key><Value><string>t</string><string>b</string></Value></KeyValueOfstringArrayOfstringty7Ep6D1></ArrayOfKeyValueOfstringArrayOfstringty7Ep6D1>

在序列化通用列表時,我遇到了類似的問題。 嘗試在序列化之前將列表更改為字符串數組string[] ,然后在反序列化之后將其更改為List<string> 這很容易做到,並且可以解決您的問題:

//List<string> to string[]
string[] sArray = sList.ToArray();

//string[] to List<string>
List<string> sList = sArray.ToList();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM