0

Trying to figure out how to best design the data structure for these 2 types of XML:

<DOCUMENTS>
    <DOCUMENT>
        <DOC_NAME>1</DOC_NAME>
        <messages>
            <message>
                <ID>1</ID>
                <MessageValue_Type_1>22222</MessageValue_Type_1>
            <message>
            <message>
                <ID>2</ID>
                <MessageValue_Type_1>3333</MessageValue_Type_1>
            <message>
        </messages>
    </DOCUMENT>

    <DOCUMENT>
        <DOC_NAME>3</DOC_NAME>
        <messages>
            <message>
                <ID>1</ID>
                <MessageValue_Type_2>AAAAAAA</MessageValue_Type_2>
            <message>
            <message>
                <ID>2</ID>
                <MessageValue_Type_2>BBBBB</MessageValue_Type_2>
            <message>
        </messages>
    </DOCUMENT>
<DOCUMENTS>

Pay attention that they are quite similar except for 2 types of MessageValue_Type_X. This is the Data structure I designed, but still can't manage to deserialize I need to override the messages properties of the base class with the none-abstract classes properties. At the end I would like to do:

XmlSerializer xmls = new XmlSerializer(typeOf(Type_1_Document));
var result = xmls.Deserialize(new StringReader(<The XML text>));

Here is the data structure:

[XmlRoot("DOCUMENT")]
public abstract class Document
{
    [XmlElement("DOC_NAME")]
    public string DocName {get; set;}
    
    [XmlIgnore]
    public virtual List<GeneralMessage> messages {get; set;}
}

public class Type_1_Document : Document
{
    [XmlElement("messages")]
    public new List<Message_Type_1> messages {get; set;}
}

public class Type_2_Document : Document
{
    [XmlElement("messages")]
    public new List<Message_Type_2> messages {get; set;}
}

[XmlRoot("message")]
public abstract class GeneralMessage
{
    [XmlElement("ID")]
    public int ID {get; set;}
}

[XmlRoot("message")]
public class Message_Type_1 : GeneralMessage
{
    [XmlElement("MessageValue_Type_1")]
    public int valueType_1 {get; set;} 
}

[XmlRoot("message")]
public class Message_Type_2 : GeneralMessage
{
    [XmlElement("MessageValue_Type_2")]
    public int valueType_2 {get; set;} 
}

SOLVED : I've used the mechanism of virtual (in abstract) and new keyword (in the inherited class) and in the abstract class virtual property I replaced the XmlAttribute with [XmlIgnore] I also removed the [XmlRoot] attribute over the abstract classes.

Guy E
  • 1,775
  • 2
  • 27
  • 55
  • You may want to fix that xml, since it is not valid. Also, `public class Type_1_Document : DOCUMENT` - case matters. You don't have a class "DOCUMENT". – Fildor Nov 09 '21 at 16:24
  • 1
    This may help https://stackoverflow.com/questions/4260338/xmlserializer-polymorphism ? – Serg Nov 09 '21 at 16:40

1 Answers1

0

SOLVED : The code is already updated with it: I've used the mechanism of virtual (in abstract) and new keyword (in the inherited class) and in the abstract class virtual property I replaced the XmlAttribute with [XmlIgnore] I also removed the [XmlRoot] attribute over the abstract classes.

Guy E
  • 1,775
  • 2
  • 27
  • 55