2

I am using WebApi2 to return XML without the namespace. I would like to remove the i: below.

<PersonDetails 
    xmlns:i="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns="http://schemas.datacontract.org/2004/07/FullJqueryForm.DTO">
    <FirstName i:nil="true" />
    <ID>0</ID>
    <LastName i:nil="true" />
    <MiddleName i:nil="true" />
    <Title i:nil="true" />
</PersonDetails>

using this so question, method 2

Added this to WebApiConfig.cs

config.Formatters.XmlFormatter.UseXmlSerializer = true;

The trouble is that I when I just return a new PersonDetails() from the controller, JSON is returned fine. But if I set the

Accept application/xml

header and then send a get request I only get the ID field back. Json however returns all fields.

public class PersonDetails
    {

        public int ID { get; set; }
        public string Title { get; set; }
        public string FirstName { get; set; }
        public string MiddleName { get; set; }
        public string LastName { get; set; }
   }

XML

<PersonDetails 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <ID>0</ID> <!-- YO WebApi!! ... Where the rest of the fields at?? -->
</PersonDetails>

Json

{
    "ID": 0,
    "Title": null,
    "FirstName": null,
    "MiddleName": null,
    "LastName": null
}

Now

I have tried setting Serializable attribute on the PersonDetails Class , didn't work.

I 'd rather not use the DataContract attribute to remove the name space as I'd then have to put loads of DataMember attributes everywhere.

I think I'm missing something simple here. Why isn't the xml formatter returning all the fields?

Thank you

Community
  • 1
  • 1
iAteABug_And_iLiked_it
  • 3,725
  • 9
  • 36
  • 75

1 Answers1

0

You'll need to decorate those properties and set XmlElement.IsNullable to true:

[XmlElement(IsNullable = true)]
public string Title { get; set; }
Yuval Itzchakov
  • 146,575
  • 32
  • 257
  • 321