Click here to Skip to main content
15,887,477 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
Hello everybody,

I have this Xml-file:

XML
<?xml version="1.0" encoding="UTF-16" standalone="no" ?>
<myNamespace:myRootnode xmlns:myNamespace="someadress/myNamespace" xmlns:meta="someotheradress/metadata" xmlns:pd="someotheradress/prodef" xmlns:ph="someotheradress/proresultHeader" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

  <pd:proDef>

  </pd:proDef>

  <ph:proResultHeader/>

  <xl:xliff version="1.1" xmlns:xl="urn:oasis:names:tc:xliff:document:1.1">
    <xl:file original="" source-language="DE-DE">
      <xl:header/>
      <xl:body/>
    </xl:file>
  </xl:xliff>

  <myNamespace:node xsi:type="myNamespace:Anlage">
    <myNamespace:node CreationDate="2016-02-29"  xsi:type="myNamespace:Maschine">
      <myNamespace:node Language="DEU - Deutsch" xsi:type="myNamespace:Allgemein"/>
      <myNamespace:node Length="500" xsi:type="myNamespace:Traverse"/>
      <myNamespace:node CoverFunction="false" xsi:type="myNamespace:Optionen"/>
      <myNamespace:node WeightWizard="true" xsi:type="myNamespace:Antriebe"/>
      <myNamespace:node xsi:type="myNamespace:Auslagerplätze">
        <myNamespace:node PlaceNumber="11" xsi:type="myNamespace:Firma">
          <myNamespace:node PlaceNumber="61" xsi:type="myNamespace:Etikettierer"/>
        </myNamespace:node>
      </myNamespace:node>
      <myNamespace:node xsi:type="myNamespace:Einlagerplätze">
        <myNamespace:node PlaceNumber="1" xsi:type="myNamespace:Einlagerplatz"/>
      </myNamespace:node>
    </myNamespace:node>
  </myNamespace:node>

</myNamespace:myRootnode>


I want to deserialize it to a C#-application but have some trouble bringing it into the correct structure.
I use an XmlSerializer for deserializing. This works till the node with
XML
xsi:type="myNamespace:Maschine"
is reached.
As seen in the Xml-file it has an attribute and some nodes under it. The attribute ist correctly deserialized but I always get just one node
XML
xsi:type="myNamespace:Einlagerplatz"
under it. If I delete the node
XML
xsi:type="myNamespace:Einlagerplätze"
from the Xml-file, the node belonging to Maschine is from
XML
xsi:type="myNamespace:Firma"
.

I cannot change the format of the Xml-file because it is generated by another application.

I used [XmlArrayItem] and stuff as you can below.

This is what happens in the main()-function:

C#
TextReader inStream = null;
myRootnode wurzel = null;
try
{
    inStream = new StreamReader(checklistPath, Encoding.Unicode);
    XmlSerializer serializerObj = new XmlSerializer(typeof(root));

    wurzel = (myRootnode)serializerObj.Deserialize(inStream);
}
catch (Exception ex)
{
    Console.WriteLine(ex.ToString());

    Console.ReadLine();

    wurzel = null;
}
finally
{
    inStream.Close();
    inStream = null;
}


These are the classes:

C#
[Serializable]
[XmlRoot(Namespace = "someadress/myNamespace")]
public class myRootnode
{
    [XmlElement("node")]
    public Anlage Anlage { get; set; }
}

[Serializable]
[XmlInclude(typeof(Anlage))]
[XmlInclude(typeof(Maschine))]
[XmlInclude(typeof(Allgemein))]
[XmlInclude(typeof(Traverse))]
[XmlInclude(typeof(Optionen))]
[XmlInclude(typeof(Antriebe))]
[XmlInclude(typeof(Auslagerplaetze))]
[XmlInclude(typeof(Firma))]
[XmlInclude(typeof(Etikettierer))]
[XmlInclude(typeof(Einlagerplaetze))]
[XmlInclude(typeof(Einlagerplatz))]
public class node
{
}

[Serializable]
[XmlRoot(Namespace = "someadress/myNamespace")]
public class Anlage : node
{
    [XmlElement("node")]
    public Maschine Maschine { get; set; }
}

[Serializable]
public class Maschine : node
{
    [XmlAttribute]
    public string CreationDate { get; set; }

    [XmlArrayItem(Type = typeof(node)),
    XmlArrayItem(Type = typeof(Allgemein)),
    XmlArrayItem(Type = typeof(Traverse)),
    XmlArrayItem(Type = typeof(Optionen)),
    XmlArrayItem(Type = typeof(Antriebe)),
    XmlArrayItem(Type = typeof(Auslagerplaetze)),
    XmlArrayItem(Type = typeof(Einlagerplaetze))]
    public node[] node { get; set; }
}

[Serializable]
public class Allgemein : node
{
    [XmlAttribute]
    public string Language { get; set; }
}

[Serializable]
public class Traverse : node
{
    [XmlAttribute]
    public string Length{ get; set; }
}

[Serializable]
public class Optionen : node
{
    [XmlAttribute]
    public string CoverFunction { get; set; }
}

[Serializable]
public class Antriebe : node
{
    [XmlAttribute]
    public string WeightWizard { get; set; }
}

[Serializable]
public class Auslagerplaetze : node
{
    [XmlArray("node")]
    [XmlArrayItem(Type = typeof(node)),
    XmlArrayItem(Type = typeof(Firma))]
    public node[] plaetze { get; set; }
}

[Serializable]
public class Einlagerplaetze : node
{
    [XmlArray("node")]
    [XmlArrayItem(Type = typeof(node)), XmlArrayItem(Type = typeof(Einlagerplatz))]
    public node[] plaetze { get; set; }
}

[Serializable]
[XmlRoot(Namespace="someadress/myNamespace")]
public class Firma: node
{
    [XmlAttribute]
    public string PlaceNumber { get; set; }

    [XmlArrayItem(Type = typeof(node)), XmlArrayItem(Type = typeof(Etikettierer))]
    public node[] node { get; set; }
}

[Serializable]
[XmlRoot(Namespace = "someadress/myNamespace")]
public class Etikettierer : node
{
    [XmlAttribute]
    public string PlaceNumber { get; set; }
}

[Serializable]
[XmlRoot(Namespace = "someadress/myNamespace")]
public class Einlagerplatz : node
{
    [XmlAttribute]
    public string PlaceNumber { get; set; }

}


What I have tried:

I tried to change the hierarchy of my C# construct and experimented with [Xml...]-"tags".

Has someone any idea where I made a mistake?
Posted
Updated 2-Mar-16 2:43am

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900