I am programming an UWP app and want to deserialize a list inside a xml to a list of Objects:
<List>
<Element Name="A">
<SubElement Name="A1">
<Color> Blue </Color>
<Color> Red </Color>
</SubElement>
<SubElement Name="A2">
<Color> Blue </Color>
<Color> Green </Color>
</SubElement>
</Element>
<Element Name="B">
<SubElement Name="B1">
<Color> Yellow </Color>
<Color> Red </Color>
</SubElement>
<SubElement Name="B2">
<Color> Yellow </Color>
<Color> Green </Color>
</SubElement>
</Element>
<Element Name="C"/>
<SubElement Name="C1">
<Color> Purple </Color>
<Color> Red </Color>
</SubElement>
<SubElement Name="C2">
<Color> Purple </Color>
<Color> Green </Color>
</SubElement>
</Element>
</List>
The classes look like this:
public class Element
{
[XmlAttribute]
public string Name { get; set; }
[XmlElement("SubElement")]
public List<SubElement> _subelement { get; set; }
}
public class SubElement
{
[XmlElement("Color")]
public string Color{ get; set; }
[XmlAttribute("Name")]
public string Name { get; set; }
}
My C# code looks like this:
XDocument doc = XDocument.Load("list.xml");
XElement node = doc.Descendants(XName.Get("List")).FirstOrDefault();
var serializer = new XmlSerializer(typeof(List<Element>), new XmlRootAttribute("List"));
var elementList = serializer.Deserialize(node.CreateReader()) as List<Element>;
In debug mode this application works fine. In release mode I get a PlatformNotSupportedException
error as in this thread. I found out that the problem must have something to do with the Compile with .NET Native tool chain
Option in the project settings. But I found no solution for the problem.
My Question: Is there a simple alternative for the code shown above to deserialize XML to Objects, which doesn't use XmlSerializer and works in an UWP app?