title: How to dynamically set IgnoreDataMemberAttribute to properties based on condition?
body: <p>I have a class like this:</p>
  
  <pre><code>public class TestClass
  {
      public int SNo { get; set; }
      public string Name { get; set; }
      public string MyProperty { get; set; }
  }
  </code></pre>
  
  <p>I am trying to serialise this class to XML using this function:</p>
  
  <pre><code>public static string ToXML(object obj)
  {
      DataContractSerializer _serializer = new DataContractSerializer(obj.GetType());
      MemoryStream msMemoryStream = new MemoryStream();
      _serializer.WriteObject(msMemoryStream, obj);
      string xml = Encoding.Default.GetString(msMemoryStream.ToArray());
      return xml;
  }
  </code></pre>
  
  <p><strong>I want output as XML.  If I pass a null value to any propery in TestClass, then that property should not come in the output XML.  How to do that?</strong></p>
  
  <h1>Update:</h1>
  
  <p>How to dynamically set IgnoreDataMemberAttribute to Testclass properties based on condition?</p>