Deserialize an XML with multiple namespaces

1 hour ago 2
ARTICLE AD BOX

I am trying to deserialize an XML file with the following structure:

<file:Process xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:MapDB="http://www.dfdse.org/mapdb" xmlns:MapDC="http://www.dfdse.org/mapdc" xmlns:file="platform:/resource/org.dfdse.model/model/File.xsd"> <node Name="ELMap"> <nodeData xsi:type="MapDB:DBMapper"> <Input name="rec_att"> <Entries name="cod_rapporto" /> <Entries name="sisalime"/> </Input> <Input name="rr_orig_rapp"> <Entries name="cod_rapporto" /> <Entries name="dt_origination"/> <Entries name="rr"/> </Input> <Output name="table3"> <Entries name="cod_rapporto" /> <Entries name="sisalime" /> <Entries name="dt_origination" /> <Entries name="dt_origination_ifrs9" /> </Output> </nodeData> </node> <node Name="Map"> <nodeData xsi:type="MapDC:DCMapper"> <outputDC size="INTERMEDIATE"> <mapperEntries name="id_ordine" /> <mapperEntries name="id_cliente" /> </outputDC> <outputDC sizeState="INTERMEDIATE"> <mapperEntries name="id_spedizione" /> <mapperEntries name="id_ordine" /> <mapperEntries name="indirizzo" /> </outputDC> <inputDC sizeState="INTERMEDIATE"> <mapperEntries name="maxIdOrdine" /> </inputDC> </nodeData> </node> </file:Process>

The issue is that I cannot get any data from the nodeData element. The problem seems to be that the same nodeData element can have two different types, defined in two different namespaces (MapDB and MapDC).

I tried to model the C# classes like this:

[XmlRoot("Process", Namespace = "platform:/resource/org.dfdse.model/model/File.xsd")] public class Process { [XmlElement("node", Namespace = "")] public List<Node> Nodes { get; set; } } public class Node { [XmlAttribute("Name")] public string Name { get; set; } [XmlElement(ElementName = "nodeData", Type = typeof(DBMapper), Namespace = "http://www.dfdse.org/mapdb")] [XmlElement(ElementName = "nodeData", Type = typeof(DCMapper), Namespace = "http://www.dfdse.org/mapdc")] public NodeBase NodeData { get; set; } } [XmlInclude(typeof(DBMapper))] [XmlInclude(typeof(DCMapper))] public class NodeBase { } //[XmlType(TypeName = "DBMapData", Namespace = "http://www.dfdse.org/mapdb")] [XmlRoot("nodeData", Namespace = "http://www.dfdse.org/mapdb")] public class DBMapper : NodeBase { [XmlElement("Input", Namespace = "")] public List<Input> Input { get; set; } } //[XmlType(TypeName = "MapperData", Namespace = "http://www.dfdse.org/mapdc")] [XmlRoot("nodeData", Namespace = "http://www.dfdse.org/mapdc")] public class DCMapper : NodeBase { [XmlElement("outputDC", Namespace = "")] public List<OutputDC> OutputDC { get; set; } } public class Input { [XmlAttribute("name")] public string Name { get; set; } } public class OutputDC { [XmlAttribute("size")] public string Size { get; set; } } public class Program { static void Main(string[] args) { var xmlFilePath = @"file.xml"; var serializer = new XmlSerializer(typeof(Process), new XmlRootAttribute("Process") { Namespace = "platform:/resource/org.dfdse.model/model/File.xsd" } ); using FileStream fileStream = new FileStream(xmlFilePath, FileMode.Open); var obj = (Process)serializer.Deserialize(fileStream); } }

However, deserialization does not work correctly, and the NodeData object is not populated. What is the correct way to handle deserialization of an XML element (nodeData) that can have multiple types (specified with xsi:type) and belong to different namespaces?

Read Entire Article