title: Using custom XmlMediaTypeFormatter to remove all namespaces in web api response c# .net
body: <p>I am running into a problem trying to remove all namespaces in my XML Web Api response. In particular I am having trouble removing nested/children namespaces. I will start out with some background.
  I have a many model classes which I have decorated with     </p>
  
  <pre><code>[XmlRoot("someRoot", Namespace = "someNameSpace")]
  </code></pre>
  
  <p>There are no other decorations in any of these classes just XmlRoots, perhaps this is not the nest approach?</p>
  
  <p>Anyways,</p>
  
  <p>I have implemented my own custom XmlMediaTypeFormatter by reading some of the answers in different forms here.
  The implementation is as follows (from <a href="https://stackoverflow.com/questions/12590801/remove-namespace-in-xml-from-asp-net-web-api">Remove namespace in XML from ASP.NET Web API</a>)</p>
  
  <pre><code>public class CustomNamespaceXmlFormatter : XmlMediaTypeFormatter
  {
      public override Task WriteToStreamAsync(Type type, object value, Stream writeStream, HttpContent content,
                                              TransportContext transportContext)
      {
          try
          {
              var xns = new XmlSerializerNamespaces();
              foreach (var attribute in type.GetCustomAttributes(true))
              {
                  var xmlRootAttribute = attribute as XmlRootAttribute;
                  if (xmlRootAttribute != null)
                  {
                      xns.Add(string.Empty, xmlRootAttribute.Namespace);
                  }
              }
  
              if (xns.Count == 0)
              {
                  xns.Add(string.Empty, string.Empty);
              }
  
              var task = Task.Factory.StartNew(() =&gt;
                  {
                      var serializer = new XmlSerializer(type);
                      serializer.Serialize(writeStream, value, xns);
                  });
  
              return task;
          }
          catch (Exception)
          {
              return base.WriteToStreamAsync(type, value, writeStream, content, transportContext);
          }
      }
  }
  </code></pre>
  
  <p>I have also registered this in my WebApiConfig.cs as:</p>
  
  <pre><code>GlobalConfiguration.Configuration.Formatters.Add(new CustomNamespaceXmlFormatter());
  
  GlobalConfiguration.Configuration.Formatters.Remove(GlobalConfiguration.Configuration.Formatters.XmlFormatter);
  </code></pre>
  
  <p>Now my issue is that the namespaces are removed but there is some weird behavior in that they are now being added to their children...</p>
  
  <p>For example, without the custom formatter my xml looks like</p>
  
  <pre><code>&lt;d2p1:MyClass xmlns:d3p1="http://schemas.datacontract.org/2004/07/MyClass"&gt;
        &lt;d3p1:ChildNodeOfMyClass&gt;
  </code></pre>
  
  <p>But when using my CustomNamespaceXmlFormatter, I would get</p>
  
  <pre><code>&lt;MyClass&gt;
    &lt;ChildNodeOfMyClass xmlns="http://schemas.datacontract.org/2004/07/MyClass"&gt;
  </code></pre>
  
  <p>This behavior occurs all throughout my response. Any ideas on why this is happening?</p>
  
  <p>As a side note I would like to add that the reason I used XmlRoot is because some of my classes contain public attributes that have the same class name and is needed so that there is no conflict when serializing my response.</p>
  
  <p>Any help/direction on how to handle this would be appreciated, thank you =)</p>