views:

359

answers:

2

I want to remove the default xmlns (namespace) added to my service's response (see image below) alt text

Is there a way to do this? This because in order to consume this service from a C# windows app, I have to add the namespace to every data object's header - DataContract that I will be serializing.

+4  A: 

I think if you just use

[DataContract(Namespace="")]

on the Bookmarks class, that should do it.


I just tried this and got the following:

<CompositeType xmlns:i="http://www.w3.org/2001/XMLSchema-instance"&gt;
  <BoolValue>true</BoolValue>
  <StringValue>Hello </StringValue>
</CompositeType>
John Saunders
It doesn't do it when Bookmarks (CollectionDataContract) is the root namespace. And also adding [DataContract] requires that every property has a [DataMember] attribute.
Tawani
@Tawani: yes, that's the recommended practice: explicitly mark your classes with [DataContract] and all your members with [DataMember]. WCF tries to encourage such an explicit coding style - your intent is clearer that way, someone else reading your code more quickly and easily understands what you're trying to do. Embrace it!
marc_s
@marc_s: I don't mind marking all the classes with [DataContract]. I just want to get rid of the root namespace in the response. That way my clients will not need to explicitly add a namespace to every object.
Tawani
@Tawani: using a namespace is really a best practice. You should use one of your own, though, and not let it default. Namespaces are especially important if some of your clients will be manipulating the XML from your service along with other XML. There can be naming conflicts if, for instance, your service has a "Book" element and so does another service, and they both want to be combined into one document. Don't you provide your clients with the XML Schema describing your XML? That will also tell them the namespace.
John Saunders
@John Saunders: I understand what you mean but it still doesn't make any sense because our clients just need to parse out data and not merge it with others. Besides out JSON clients don't have a namespace issue.
Tawani
@Tawani: JSON is used for almost nothing except web pages. XML has many more uses, so it's much more likely to have two XML documents combined. If you're sure your clients are that limited, then go ahead. The world has more in it than just web clients.
John Saunders
A: 

Actually, WCF REST Contrib proved to be the perfect solution using their PoxDataContract.

Tawani