Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am using ASP.Net Web API (WCF 4.0) method to return a List<WorkItem>.

This is returning an xml with ArrayOf... in the form

<ArrayOfworkitem xmlns="http://schemas.datacontract.org/2004/07/AgilePortalServices.DataContracts" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
    <workitem>
        <id>28</id>
        <title>Test</title>
    </workitem>
    <workitem>
        <id>27</id>
        <title>Test Bug</title>
    </workitem>
</ArrayOfworkitem>

But I want it returned as

<workitems>
    <workitem>
        <id>28</id>
        <title>Test</title>
    </workitem>
    <workitem>
        <id>27</id>
        <title>Test Bug</title>
    </workitem>
</workitems>

How do I do this?

share|improve this question

1 Answer

up vote 1 down vote accepted

This will be due to the serializer using the WCF XML serializer, instead of the default XmlSerializer.

You can modify this by stting the default formatters (and you can replace this with a 3rd party if you choose to).

var xml = GlobalConfiguration.Configuration.Formatters.XmlFormatter;
xml.UseXmlSerializer = true;

More info at this web-api overview

share|improve this answer
Where do I put this code - I studied the link which suggests the Application_start method in Global.asax.cs but I get an Error: The name 'GlobalConfiguration' does not exist in the current context – Kevin Sep 4 '12 at 14:44
I just re-read your question. Apologies, are you using WCF4.0? Or WebAPI? You should create a new WebAPI (ASP.Net 4.0) Project, not a WCF project. – leon Sep 4 '12 at 17:05
Wasn't using a WebAPI project. Will try that now. Thanks – Kevin Sep 4 '12 at 21:27

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.