Feedback on ASP.NET Web API

Remove Ambient Namespaces From Serialized XML

Currently the XML serialisers both output namespaces that are not required:

xmlns:xsd="http://www.w3.org/2001/XMLSchema"; xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";

I'm not sure about the Data Contract serializer, but it is not difficult to supress these...

Just pass into the Serialize method an XmlSerializerNamespaces:

var xmlSerializerNamespaces = new XmlSerializerNamespaces();
xmlSerializerNamespaces.Add(String.Empty, defaultNamespace);

var xmlSerializer = new XmlSerializer(objectType);
xmlSerializer.Serialize(xmlWriter, toBeSerialised, xmlSerializerNamespaces);

Where defaultNamespace could be set on the GlobalConfiguration.Configuration.Formatters.XmlFormatter or it could be read from the XmlRoot attribute (if present) on the object being serialised.

25 votes
Vote
Sign in
Check!
(thinking…)
Reset
or sign in with
  • facebook
  • google
    Password icon
    I agree to the terms of service
    Signed in as (Sign out)
    You have left! (?) (thinking…)
    PiersPiers shared this idea  ·   ·  Flag idea as inappropriate…  ·  Admin →

    1 comment

    Sign in
    Check!
    (thinking…)
    Reset
    or sign in with
    • facebook
    • google
      Password icon
      I agree to the terms of service
      Signed in as (Sign out)
      Submitting...
      • PiersPiers commented  ·   ·  Flag as inappropriate

        For "but it is not difficult to supress these" read "but it is not difficult to supress these on the XmlSerializer"

      Feedback and Knowledge Base