title: How to dynamically choose what inherited properties will be serialized?
body: <p>I have a base class with a few properties, and three deriving classes.</p>
  
  <p>I want to serialize an object containing all three derived classes, but each derived class should expose a different set of properties from base class.</p>
  
  <p>I want to do this dynamically with XmlAttributeOverrides and have tried a few differnt ways to do that, but nothing that really does it.</p>
  
  <pre><code>[Serializable]
  public class A
  {
      public string Property1 { get; set; }
      public string Property2 { get; set; }
  }
  
  [Serializable]
  public class B : A
  {
  }
  
  [Serializable]
  public class C : A
  {
  }
  
  [Serializable]
  public class Container
  {
      public B B { get; set; }
      public C C { get; set; }
  }
  
  
  class Program
  {
      static void Main(string[] args)
      {
          MemoryStream memoryStream = new MemoryStream();
          StreamWriter encodingWriter = new StreamWriter(memoryStream, Encoding.Unicode);
  
          var xmlWriter = XmlWriter.Create(encodingWriter, new XmlWriterSettings
          {
              Indent = false,
              OmitXmlDeclaration = true,
          });
          XmlAttributeOverrides overrides = new XmlAttributeOverrides();
          XmlAttributes attribute = new XmlAttributes();
          attribute.XmlIgnore = true;
          overrides.Add(typeof(B), "Property1", attribute);
          overrides.Add(typeof(C), "Property2", attribute);
          var container = new Container
              {
                  B = new B {Property1 = "B property 1", Property2 = "B property 2"},
                  C = new C {Property1 = "C property 1", Property2 = "C property 2"}
              };
  
          var xmlSerializer = new XmlSerializer(typeof(Container), overrides);
          xmlSerializer.Serialize(xmlWriter, container);
  
          var result = Encoding.Unicode.GetString(memoryStream.ToArray());
      }
  }
  </code></pre>
  
  <p>In the above code, the result string will contain all the properties of A in B and C, but I really want it just to contain B Property2 and C Property1 (as I have set the XmlIgnore attributes for them).</p>
  
  <p>How do I do this?</p>
  
  <p>EDIT: The expected XML:</p>
  
  <pre><code>&lt;Container xmlns:xsd="http://www.w3.org/2001/XMLSchema"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&gt;&lt;B&gt;&lt;Property2&gt;B property 2&lt;/Property2&gt;&lt;/B&gt;&lt;C&gt;&lt;Property1&gt;C property 1&lt;/Property1&gt;&lt;/C&gt;&lt;/Container&gt;
  </code></pre>
  
  <p>The actual XML:</p>
  
  <pre><code>&lt;Container xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&gt;&lt;B&gt;&lt;Property1&gt;B property 1&lt;/Property1&gt;&lt;Property2&gt;B property 2&lt;/Property2&gt;&lt;/B&gt;&lt;C&gt;&lt;Property1&gt;C property 1&lt;/Property1&gt;&lt;Property2&gt;C property 2&lt;/Property2&gt;&lt;/C&gt;&lt;/Container&gt;
  </code></pre>
  
  <p>EDIT 2: The above is an example to visualize the problem, but I will expand on why we have the need to do this.</p>
  
  <p>We have a Container-class (like above) which contains different kinds of derived objects (like above).
  When we expose data from the Container-class to others, we want to be able to expose just certain fields which are configurable at some other place (might be sensitive data or whatnot). </p>
  
  <p>This we do with help of XmlAttributeOverrides to set XmlIgnore property for the exposed properties. This works well for most types of objects (which does not have inheritance), but now we have the need to serialize different derived objects in different ways (configurable). </p>
  
  <p>So in the above example, some customer has made the decision to exclude Property1 from class B and Property2 from class C, and as such I want the XML to look like above.
  This did not work with the above code though; it seems like XmlSerializer uses the settings for the properties from the base class A, instead of using it from the respective derived classes B and C.</p>