0

I have generic base class which has property body and another derived class which has property header and when i deserialize the derived class into xml its placing the body as first element first then header (deserializing in alphabetical order) but I want header first and then Body. I have tried XmlElement's order property but its not working

Sample class:

[XmlRoot("BaseClass", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
public class BaseClass<T>
{
   public T Body { get; set; }
}

[XmlRoot("DerivedClass", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
public class DerivedClass<THeader, TBody> : BaseClass<TBody>
where THeader : IEnvelopeHeader where TBody : class, IEnvelopeBody, new()
{
    public THeader Header { get; set; }
}

I saw one solution which says that make the property in base class as abstract and override in derived class then decide the order but i cann't go with this solution which my base class is used in some other projects as well.

Sudama Tripathi
  • 349
  • 1
  • 5
  • 20

1 Answers1

0

I have fixed the order issue for now by removing the inheritance but that's not what i wanted to achieve, All I need is to order the xml element as per the order defined even when you are using inheritance chain.

[XmlRoot("Envelope", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
public class Envelope<THeader, TBody>
where THeader : IEnvelopeHeader where TBody : class, IEnvelopeBody, new()
{
    [XmlElement(ElementName = "Header", Order = 1)]
    public THeader Header { get; set; }

    [XmlElement(ElementName = "Body", Order = 2)]
    public TBody Body { get; set; }
}
Sudama Tripathi
  • 349
  • 1
  • 5
  • 20