0

Let say I have following classes:

public class Entity { ... }

public class MyEntity : Entity 
{
}

MyEntity can be complex object, which has list of Entity as its property. I'd like to serialize MyEntity to xml, but only properties of base class, i.e Entity object. I tried to use DataContractSerializer with DataMemberAttribute, but it seems, starting with .NET 3.5 it serializes all public properties even if DataMemberAttribute is not applied.

What are my options?

synergetic
  • 7,756
  • 8
  • 65
  • 106

1 Answers1

0

Okay, answer to my own question. My current solution is to implement IXmlSerializable in the base class (Entity class), and not implement this interface for derived classes (MyEntity). Then XmlSerializer serializes public properties of only the base class. Tricky part is implementing IXmlSerializable; especially ReadXml() method. An answer by Paul Alexander (not selected answer!) in the following post was helpful to me: Reading Xml with XmlReader in C#

Community
  • 1
  • 1
synergetic
  • 7,756
  • 8
  • 65
  • 106