Removing XML namespace from WebApi

I am working with WebApi and XML as a result.

I just keep getting weird namespace in my result like:

<QueryId>FE04A4E6-A584-47BF-9DA1-7360DFB08A8D</QueryId>
<ExecutionError>true</ExecutionError>
<OperationResult xmlns:d2p1="http://schemas.datacontract.org/2004/07/MyTypes" i:nil="true"/>
<ErrorMessage>INVALID ACCOUNT</ErrorMessage>

      

I read some solutions and included this in my WebApiConfig.cs:

config.Formatters.XmlFormatter.UseXmlSerializer = true;

      

But using this parameter, my return was the same without the OperationResult tag (the only one that was not populated):

<QueryId>FE04A4E6-A584-47BF-9DA1-7360DFB08A8D</QueryId>
<ExecutionError>true</ExecutionError>
<ErrorMessage>INVALID ACCOUNT</ErrorMessage>

      

I tried to use my result object like this:

[DataContract(Namespace = "")]
public class CustomRecordResult
{
    [DataMember]
    public string             QueryId         { get; set; }
    [DataMember]
    public bool               ExecutionError  { get; set; }
    [DataMember]
    public string             ErrorMessage    { get; set; }
    [DataMember]
    public CustomSourceRecord OperationResult { get; set; }
}

      

But it doesn't work. In this case my result object is empty

Any ideas?

Many thanks!

+3


source to share


1 answer


Use the EmitDefaultValue parameter to control whether this member is serialized when it is zero.



[DataMember(EmitDefaultValue = false)]
public CustomSourceRecord OperationResult { get; set; }

      

+1


source







All Articles