简体   繁体   中英

ASP.NET Core How to output custom XML Model

I have a Web API built in C# core and I need to customize the XML output that one of my endpoints gives.

To configure my app to return XML and JSON, I use Accept Header like so:

        // Add framework services.
        services
            .AddMvc(options => {
                options.RespectBrowserAcceptHeader = true;
            })
            //support application/xml
            .AddXmlDataContractSerializerFormatters()
            //support application/json
            .AddJsonOptions(options => {
                options.SerializerSettings.ContractResolver = new DefaultContractResolver();
            });

And I have a model I return on a controller:

public class SageInvoice {

    public long Id { get; set; }

    public DateTime Created { get; set; }

    public long? CustomerId { get; set; }

    public long? JobId { get; set; }

    public DateTime Date { get; set; }

    public decimal Subtotal { get; set; }

    public decimal VAT { get; set; }

    public decimal Total { get; set; }
}

The XML comes out like so:

<ArrayOfSageInvoice xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/AutomotiveDTO.DTOs.SageIntegration">
    <SageInvoice>
        <Created>2017-03-15T11:09:34.21</Created>
        <CustomerId>1</CustomerId>
        <Date>2017-03-15T00:00:00</Date>
        <Id>2</Id>
        <JobId>1</JobId>
        <Subtotal>100.00</Subtotal>
        <Total>120.00</Total>
        <VAT>20.00</VAT>
    </SageInvoice>
    <SageInvoice>
        <Created>2017-03-15T11:11:26.853</Created>
        <CustomerId>2</CustomerId>
        <Date>2017-03-15T00:00:00</Date>
        <Id>3</Id>
        <JobId i:nil="true"/>
        <Subtotal>23532.00</Subtotal>
        <Total>28238.40</Total>
        <VAT>4706.40</VAT>
    </SageInvoice>
</ArrayOfSageInvoice>

The consumer of my endpoint doesn't like it structured like this and wants me to make it look more:

<?xmlversion="1.0"encoding="utf-8"?>
<Order>
    <OrderSummary>
        <OrderID>1</OrderID>
        <OrderReference>ORDER1</OrderReference>
        <OrderDate>2017-03-15</OrderDate>
        <AccountNumber>ACCOUNT1</AccountNumber>
        <CompanyName>Company 1</CompanyName>
        <ContactTitle>Mr</ContactTitle>
        <ContactFirst>Dave</ContactFirst>
        <ContactLast>Dave</ContactLast>
        <Address1/>
        <Address2/>
        <Town/>
        <County/>
        <Postcode/>
        <Country/>
        <Telephone>01234567890</Telephone>
        <NumberOfItems>2</NumberOfItems>
        <OrderValue>200</OrderValue>
        <Downloaded>false</Downloaded>
    </OrderSummary>
    <OrderSummary>
    ...
    </OrderSummary>
</Order>

How can I change my XML output to match the above without wrecking the Accept headers JSON + XML input/output functionality?

This should do it (see MSDN ). The docs are for .NET Framework, but should also apply to .NET Core.

[CollectionDataContract(Name = "Invoice", ItemName = "InvoiceSummary")]
public class SageInvoice
{
    ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM