0

I have to implement some protocol. All of xmls that I need to serialize(WCF contracts) have always two specified sections and one "content" section - e.g.:

First:

<X>
 <Y>
  <Content1/>
 </Y>
</X>

Second:

<X>
  <Y>
   <Content2/>
  </Y>
</X>

I want to use some generics, but then problem with node name appeared. I found this answer : Serialization DataMember (name) override issue but it doesn't fit for me.

Code for my approach:

public class X<TContent>
{
     [DataMember]
     public Y<TContent> yName { get; set; }
}

public class Y<TContent>
{
     [DataMember]
     public virtual TContent Content { get; set; }
}

And then there is few "Content" classes like:

public sealed class Content1Class: Y<Content1>
{
    [DataMember(Name = nameof(Content1))]
    public override Content1 Content { get; set; }
}

Unfortunately I have always null for Content.

My question is: Can I set attributes and relations between classes to deserialize the above-mentioned case? Maybe I have to create separate classes for each content type?

dbc
  • 104,963
  • 20
  • 228
  • 340
Cytryn
  • 1
  • 3
  • `DataContractSerializer` doesn't support element polymorphism via changing the element name. You'll need to change your design or implement `IXmlSerializable` for `Y`, the latter of which is a nuisance and not recommended. Let me know if you want answers for either approach. – dbc Apr 27 '19 at 19:53
  • Thank you for your response. I should change my design. The simplest approach would be to create separate classes for each case. Do you have any other idea? I can't change the xml structure. – Cytryn Apr 28 '19 at 08:36

0 Answers0