1

非常にうまく機能するシリアライズ可能な辞書を見つけました:http://www.jankowskimichal.pl/en/2010/10/serializabledictionary/

しかし、ディクショナリ内のオブジェクトの 1 つが単に文字列のリストである場合は常に例外が発生します。.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 で何かが足りないのかもしれません。

編集: odyss の例を試すと、次のようになります:

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.
4

2 に答える 2

0

As an alternative, you might have better luck with System.Runtime.Serialization.DataContractSerializer. Example:

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);
}

This generates:

<?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>
于 2013-01-31T20:08:05.980 に答える
0

ジェネリック リストのシリアル化で同様の問題が発生しました。シリアル化する前にリストを文字列配列に変更し、逆シリアル化後にリストにstring[]戻してみてくださいList<string>。これは非常に簡単で、問題を解決する可能性があります。

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

//string[] to List<string>
List<string> sList = sArray.ToList();
于 2013-01-31T20:21:56.243 に答える