title: XmlSerializer ignoring attribute overrides when serializing
body: <p>I have a complex graph and each and every property has a custom attribute. Once I have an object, I traverse the properties, check the custom attribute and if necessary, add the property to be ignored:</p>
  
  <pre><code>var ignoreAttribute = new XmlAttributes() {
      XmlIgnore = true
  };
  var overrides = new XmlAttributeOverrides();
  
  var objType = obj.GetType();
  var properties = objType.GetProperties();
  
  foreach (var property in properties)
  {
      var attrib = property.GetCustomAttribute&lt;ExportLevelAttribute&gt;();
      if (!attrib.Values.Contains(level))
      {
          try
          {
              var element = property.GetCustomAttribute&lt;XmlElementAttribute&gt;();
              var name = element?.ElementName == string.Empty ? property.Name : element.ElementName;
  
              overrides.Add(property.PropertyType, name, ignoreAttribute);
          }
          catch { }
  
          continue;
      }
  }
  
  using (var stream = new MemoryStream())
  using (var writer = new StreamWriter(stream, new UTF8Encoding(false)))
  {
      var xml = new XmlSerializer(typeof(MyRootObject), overrides);
      xml.Serialize(writer, obj);
  
      return Encoding.UTF8.GetString(stream.ToArray());
  }
  </code></pre>
  
  <p>Properties are being added to the overrides. However, when serializing, those properties which are set to be ignored are still being serialized.</p>
  
  <p>Am I missing something?</p>