I need to (de)serialize a class in C# (.NET Framework 4.5.2) to and from XML which has a dictionary property with string
keys and string[]
array values. I am using the SerializableDictionary<TKey, TValue>
implementation mentioned in this answer on another question, so that means my property is of type SerializableDictionary<string, string[]>
.
Serialization of this to an XML file looks like it works alright; however, deserializing always fails with a System.InvalidOperationException.
This occurs even when just deserializing the dictionary on its own. See the below MSTest unit test which reproduces the problem:
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"));
}
}
}
The exception is at the serializer.Deserialize
call, and reads:
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.
Why am I getting this exception and how can I avoid it? I would like to avoid having to resort to custom XML serialization for just this one property, if possible.
Edit #1:
I made a small console program which runs the above construction and serialization code, then writes the resulting XML to a file using the FileStream
class. Here is its contents:
<?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>
Edit #2:
After some additional testing changing the TValue
type of the SerializableDictionary<TKey, TValue>
declaration in my tests to different things and check whether it works at all, I can confirm that it works okay if TValue
is string
, but the same error results if it is set to, say, a custom serializable class of mine, even if that class is decorated with SerializableAttribute
or implements the IXmlSerializable
interface. So, the problem is actually bigger than just with string arrays.