gpt4 book ai didi

c# - 如何(反)序列化具有字符串数组值的可序列化字典?

转载 作者:行者123 更新时间:2023-11-30 23:04:32 26 4
gpt4 key购买 nike

我需要将 C# (.NET Framework 4.5.2) 中的一个类与 XML 序列化(反序列化),该类具有 string 的字典属性。键和 string[]数组值。我正在使用 SerializableDictionary<TKey, TValue> this answer 中提到的实现在另一个问题上,这意味着我的属性(property)类型为 SerializableDictionary<string, string[]> .

将其序列化为 XML 文件看起来工作正常;但是,反序列化总是因 System.InvalidOperationException 而失败。

即使只是反序列化字典本身也会发生这种情况。请参阅下面重现问题的 MSTest 单元测试:

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using My.Namespace.Collections; // Where SerializableDictionary is defined.
using System.Xml.Serialization;
using System.IO;
using System.Linq;

namespace My.Namespace.Collections.Tests
{
/// <summary>
/// Provides unit test methods for the <see cref="SerializableDictionary{TKey, TValue}"/> class.
/// </summary>
[TestClass]
public class SerializableDictionaryTests
{
/// <summary>
/// A basic test that the <see cref="SerializableDictionary{TKey, TValue}"/> class has working
/// (de)serialization.
/// </summary>
[TestMethod]
[Timeout(100)]
public void SerializableDictionary_BasicTest()
{
// Arrange.
var sourceDictionary = new SerializableDictionary<string, string[]>();
SerializableDictionary<string, string[]> destinationDictionary;
var serializer = new XmlSerializer(typeof(SerializableDictionary<string, string[]>));

var list1 = new string[] { "apple", "banana", "pear" };
var list2 = new string[] { "carrot", "broccoli", "cauliflower", "onion" };
var list3 = new string[] { "beef", "chicken" };

sourceDictionary.Add("fruits", list1);
sourceDictionary.Add("vegetables", list2);
sourceDictionary.Add("meats", list3);

// Act.
using (var stream = new MemoryStream())
{
serializer.Serialize(stream, sourceDictionary);
stream.Position = 0;
destinationDictionary = (SerializableDictionary<string, string[]>)serializer.Deserialize(stream);
}

// Assert.
// NOTE: We don't get this far because it crashes on the last statement above in the using block.
Assert.AreEqual(3, destinationDictionary.Keys.Count);
Assert.IsTrue(destinationDictionary.ContainsKey("fruits"));
Assert.IsTrue(destinationDictionary.ContainsKey("vegetables"));
Assert.IsTrue(destinationDictionary.ContainsKey("meats"));

Assert.AreEqual(3, destinationDictionary["fruits"].Length);
Assert.IsTrue(destinationDictionary["fruits"].Contains("apple"));
Assert.IsTrue(destinationDictionary["fruits"].Contains("banana"));
Assert.IsTrue(destinationDictionary["fruits"].Contains("pear"));

Assert.AreEqual(4, destinationDictionary["vegetables"].Length);
Assert.IsTrue(destinationDictionary["vegetables"].Contains("carrot"));
Assert.IsTrue(destinationDictionary["vegetables"].Contains("broccoli"));
Assert.IsTrue(destinationDictionary["vegetables"].Contains("cauliflower"));
Assert.IsTrue(destinationDictionary["vegetables"].Contains("onion"));

Assert.AreEqual(2, destinationDictionary["meats"].Length);
Assert.IsTrue(destinationDictionary["meats"].Contains("beef"));
Assert.IsTrue(destinationDictionary["meats"].Contains("chicken"));
}
}
}

异常是在 serializer.Deserialize打电话,读:

Test method My.Namespace.Collections.Tests.SerializableDictionaryTests.SerializableDictionary_BasicTest threw exception: 
System.InvalidOperationException: There is an error in XML document (8, 8). ---> System.InvalidOperationException: There is an error in XML document (8, 8). ---> System.InvalidOperationException: <ArrayOfString xmlns=''> was not expected.

为什么会出现此异常,我该如何避免?如果可能的话,我想避免只为这一个属性求助于自定义 XML 序列化。

编辑#1:

我制作了一个运行上述构建和序列化代码的小型控制台程序,然后使用 FileStream 将生成的 XML 写入文件。类(class)。这是它的内容:

<?xml version="1.0"?>
<dictionary>
<item>
<key>
<string>fruits</string>
</key>
<value>
<ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<string>apple</string>
<string>banana</string>
<string>pear</string>
</ArrayOfString>
</value>
</item>
<item>
<key>
<string>vegetables</string>
</key>
<value>
<ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<string>carrot</string>
<string>broccoli</string>
<string>cauliflower</string>
<string>onion</string>
</ArrayOfString>
</value>
</item>
<item>
<key>
<string>meats</string>
</key>
<value>
<ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<string>beef</string>
<string>chicken</string>
</ArrayOfString>
</value>
</item>
</dictionary>

编辑#2:

经过一些额外的测试更改 TValue SerializableDictionary<TKey, TValue> 的类型在我的测试中声明不同的东西并检查它是否有效,如果TValue,我可以确认它工作正常是string , 但如果将它设置为我的自定义可序列化类,即使该类装饰有 SerializableAttribute,也会出现相同的错误。或实现 IXmlSerializable界面。所以,问题实际上比字符串数组更大。

最佳答案

在为此苦苦挣扎了几天之后,我得出的结论是 Serializable<TKey, TValue>我使用的实现无法修改以处理数组和其他对象。

我求助于实现 IXmlSerializable我类的界面,字典值序列化为 WriteXml 中的列表方法。在 ReadXml ,我简单地反序列化存储的列表,并将其值放回字典中,同时检查是否没有重复键存储为列表项的附加属性。它很笨重,但很管用。

关于c# - 如何(反)序列化具有字符串数组值的可序列化字典?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49368839/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com