Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove namespace in XML from ASP.NET Web API

How do I remove the namespace from the xml response below using Web API?

<ApiDivisionsResponse xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/GrassrootsHoops.Models.Api.Response"> <Divisions xmlns:d2p1="http://schemas.datacontract.org/2004/07/GrassrootsHoops.Data.Entities"> <d2p1:Page>1</d2p1:Page> <d2p1:PageSize>10</d2p1:PageSize> <d2p1:Results xmlns:d3p1="http://schemas.datacontract.org/2004/07/GrassrootsHoops.Models.Api.Response.Divisions"/> <d2p1:Total>0</d2p1:Total> </Divisions> </ApiDivisionsResponse> 
like image 407
Mike Flynn Avatar asked Sep 25 '12 20:09

Mike Flynn


People also ask

How do I remove namespace prefix?

Once a namespace prefix is created, it cannot be changed or deleted. The workaround is to move all your code to a new Developer Organization, where you can setup the desired Namespace Prefix.

Why do we need namespace in XML?

One of the primary motivations for defining an XML namespace is to avoid naming conflicts when using and re-using multiple vocabularies. XML Schema is used to create a vocabulary for an XML instance, and uses namespaces heavily.

How can name conflicts be avoided in XML?

Name conflicts in XML can easily be avoided using a name prefix. In the example above, there will be no conflict because the two <table> elements have different names.

What is namespace prefix in XML?

The namespace prefix is used as an alias for the complete namespace identifier. The parser sets XML-NAMESPACE-PREFIX before transferring control to the processing procedure when the operand of the XML PARSE statement is an alphanumeric data item and the RETURNING NATIONAL phrase is not specified.


1 Answers

Option 1 is to switch to using XmlSerializer in GlobalConfiguration:

config.Formatters.XmlFormatter.UseXmlSerializer = true; 

Option 2 is to decorate your models with

[DataContract(Namespace="")] 

(and if you do so, you'd need to decorate the members with [DataMember] attributes).

like image 57
Filip W Avatar answered Sep 30 '22 06:09

Filip W