1

I'm having a hard time deserializing a HashSet of enums. Currently the values from the XML are not being deserialized into the c# object.

I have an xml file that has this:

<MyHashSetOfEnums xmlns:a="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
  <a:MyEnum>Red</a:MyEnum>
  <a:MyEnum>Green</a:MyEnum>
  <a:MyEnum>Blue</a:MyEnum>
</MyHashSetOfEnums>

And the c# class I'm trying to deserialize them into looks something like this:

[DataContract]
public class MyClass
{
    [DataMember]
    public HashSet<MyEnum> MyHashSetOfEnums { get; private set; }
}

Enum looks like this:

public enum MyEnum
{
    Red,
    Green,
    Blue
}

I have working code that deserializes a single enum and another that deserializes a HashSet of strings/ints. Both of these use different XML namespaces to deserialize correctly. But now I need to combine the two ideas and am unable to get something working. Is this possible? Or am I shooting for something unreachable here?

PS: I am not using Newtonsoft and cannot upgrade our serialization utilities to use Newtonsoft

st0ve
  • 519
  • 3
  • 18
  • It looks like you are using `DataContractSerializer` not `XmlSerializer`. Can you confirm please, and share the code you are using to (de)serialize? Also, can you share a complete XML file, not just a fragment -- i.e. a [mcve]? Your `MyClass` would serialize to an XML document with a root element ``, which is not shown in the question. We would need to see the root element name and namespace(s) at a minimum. – dbc Nov 11 '18 at 15:41

1 Answers1

0

You can try specifying the Namespaces directly, but here's a working serialization (no DataContract attributes but they shouldn't change). Added constructor for the private set MyHashSetOfEnums.

using System;
using System.IO;
using System.Collections.Generic;
using System.Xml.Serialization;

public enum MyEnum
{
    Red,
    Green,
    Blue
}
public class MyClass
{
    public HashSet<MyEnum> MyHashSetOfEnums { get; private set; }
    public MyClass() {
        MyHashSetOfEnums = new HashSet<MyEnum>(); 
    }
}
public class Program
{
    public static void Main()
    {
        var set = new MyClass();
        set.MyHashSetOfEnums.Add(MyEnum.Blue);
        set.MyHashSetOfEnums.Add(MyEnum.Red);

        var xs = new XmlSerializer(typeof(MyClass));
        string xml;
        using (var writer = new StringWriter()) {
            xs.Serialize(writer, set);
            xml = writer.ToString();
            Console.WriteLine(xml);
        }
        using (var reader = new StringReader(xml)) {
            var set2 = (MyClass)xs.Deserialize(reader);
            foreach (MyEnum s in set2.MyHashSetOfEnums)
                Console.WriteLine(s);
        }
    }
}

Here's the stdout:

<?xml version="1.0" encoding="utf-16"?>
<MyClass xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <MyHashSetOfEnums>
    <MyEnum>Blue</MyEnum>
    <MyEnum>Red</MyEnum>
  </MyHashSetOfEnums>
</MyClass>
Blue
Red

Your XML root node might need to be MyClass.

Steven Huang
  • 490
  • 4
  • 16