ASP.Net Web API XML Serialization ArrayOf

2019-08-11 11:44发布

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条回答
闹够了就滚
2楼-- · 2019-08-11 12:23

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

查看更多
登录 后发表回答