iMortalitySX
iMortalitySX

Reputation: 1488

Remove xml namespaces from WCF restful response on containing and base objects

.NET 3.5

So I have come to asking this after some frustration. The primary issue that I have is that SOME of my properties in a class that is being serialized ARE including the xmlns attibute in the XML.

The particular properties are on the base class of the object or on objects that are inside the object. I have full control over these and have added the datacontract(NameSpace="") attribute to all of my data contracts.

I am not worried about the top level xmlns or the xmlns:i, but only the lower level ones in the classes.

See Remove xml namespaces from WCF restful response

[DataContract(Namespace = "")]
BaseClass
{
    [DataMember]
    public string Name{get;set;}
}

[DataContract(Namespace = "")]
OtherClass
{
    [DataMember]
    public decimal Income{get;set;}
}

[DataContract(Namespace = "")]
DerivedClass
{
    [DataMember]
    public int Age{get;set;}

    [DataMember]
    public OtherClass Foo{get;set;}
}

On serialization of the Derived class, this is what I get in my XML.

<DerivedClass
    xmlns="http://IDontCareAboutThisOne"
    xmlns:i="http://IDontCareAboutThisOneEither">
    <Name xmlns="">iMortalitySX</Name>
    <Age>30</Age>
    <Foo>
        <Income xmlns="">0.0</Income>
    </Foo>
</DerivedClass>

This is of course just an example. Additionally, if you are curious I have done this through WCF services and using the DataContractSerializer directly with the same results (as it should be). I need this because of the amount of data being returned on an older network. Every little bit I shave makes a huge difference.

Yes, I have considered JSON, but customer requirements have driven XML. There are other reasons that I don't agree with either, but it needs to be in XML. Also, just FYI a third party serializer is not an option on this one. I am stuck with MS base stuff. I am exposing this as a WebHttpBinding with the WebBehavior, as a REST type service. I am NOT using the REST starter kit, nor will I.

Upvotes: 1

Views: 3702

Answers (2)

Simon_Weaver
Simon_Weaver

Reputation: 146170

Watch out if you're using something like Fiddler to inspect your message because its XML view will ADD in namespaces trying to make it easier for you. However those namespaces aren't necessarily present in the original raw view.

I made the mistake of trying to research this problem when I didn't need to.

Xml view

Fiddler has added the namespace here under notes.

enter image description here

Raw view

The same node the namespace isn't present

enter image description here

Now I'm not saying there are never any namespaces in the raw message that I don't really need - just that the situation may not be as bad as you fear if you're using Fiddler to inspect the message.

Upvotes: 1

Sergey Slepov
Sergey Slepov

Reputation: 2121

Try using the ContractNamespaceAttribute to move all your contract members to the same XML namespace. That will get rid of the xmlns attributes on individual members.

Upvotes: 2

Related Questions