1

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?

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

3 Comments

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
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.
Wasn't using a WebAPI project. Will try that now. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.