title: Ignore a property when serializing to XML
body: <p>I have a base class that has MANY big classes inside it.</p>
  
  <p>For example, Let's say <code>Person</code> class. Inside it, there is a <code>Payment</code> Class, inside there is a <code>CreditCard</code> class, and so on...</p>
  
  <p>I am trying to serialize <code>Person</code> class, and I would like to exclude certain classes inside it.</p>
  
  <p>In this example, I am trying to serialize <code>Person</code> class and ignore the ENTIRE payment class. This is what I did so far, but it's not working.</p>
  
  <p>Can you guys help me figure out how I can achieve this? Thanks</p>
  
  <pre><code>        XmlAttributes att = new XmlAttributes { XmlIgnore = true };
          XmlAttributeOverrides xOver = new XmlAttributeOverrides();
          xOver.Add(typeof(Payment), "Payment", att);
          SerializeContractsRequest(items, xOver);
  
  public static string Serialize&lt;T&gt;(T clrObject, XmlAttributeOverrides xmlOverrides) where T : class, new()
  {
      XmlSerializer xs = xmlOverrides == null ? new XmlSerializer(typeof(T)) : new XmlSerializer(typeof(T), xmlOverrides);
      string xml = string.Empty;
  
      //A string builder that will hold the converted business object as an xml string
      StringBuilder sb = new StringBuilder();
  
      //The stream that will write the serialized xml to the stringbuilder object
      XmlWriterSettings settings = new XmlWriterSettings();
      settings.Encoding = Encoding.UTF8;
  
      XmlWriter writer = XmlWriter.Create(sb, settings);
  
      xs.Serialize(writer, clrObject);
  
      xml = sb.ToString();
  
      return xml;
  }
  </code></pre>
  
  <p>Also, I am not allowed to touch the <code>Payment</code> Class XML Attribute. For example, I am not allowed to add <code>[XmlIgnore]</code> on <code>Payment</code> Class.</p>
  
  <p>I only need this on one method so that's where I would like to apply the override. However, just for your reference, this is what <code>Payment</code> class has:</p>
  
  <pre><code>    [System.Diagnostics.DebuggerStepThroughAttribute()]
      [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization", "4.0.0.0")]
      [System.Runtime.Serialization.DataContractAttribute(Name="Payment", Namespace="http://schemas.datacontract.org/2004/07/ServicesLayer")]
      [System.SerializableAttribute()]
  public partial class Payment
  {
  
  }
  </code></pre>