Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ASP.NET内核如何输出自定义XML模型_C#_Xml_Asp.net Core_Formatting - Fatal编程技术网

C# ASP.NET内核如何输出自定义XML模型

C# ASP.NET内核如何输出自定义XML模型,c#,xml,asp.net-core,formatting,C#,Xml,Asp.net Core,Formatting,我有一个内置于C内核的Web API,我需要自定义我的一个端点提供的XML输出 为了将我的应用程序配置为返回XML和JSON,我使用如下的Accept标头: // Add framework services. services .AddMvc(options => { options.RespectBrowserAcceptHeader = true; })

我有一个内置于C内核的Web API,我需要自定义我的一个端点提供的XML输出

为了将我的应用程序配置为返回XML和JSON,我使用如下的Accept标头:

        // Add framework services.
        services
            .AddMvc(options => {
                options.RespectBrowserAcceptHeader = true;
            })
            //support application/xml
            .AddXmlDataContractSerializerFormatters()
            //support application/json
            .AddJsonOptions(options => {
                options.SerializerSettings.ContractResolver = new DefaultContractResolver();
            });
我有一个在控制器上返回的模型:

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; }
}
XML是这样产生的:

<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>
我的端点的使用者不喜欢这样的结构,希望我让它看起来更像:

<?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>
如何在不破坏Accept headers JSON+XML输入/输出功能的情况下更改XML输出以匹配上述内容?

应该这样做,请参见。这些文档适用于.NET Framework,但也应适用于.NET Core

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

您可以使用[Xml…Atribute]来更改标记的名称,当然,标记仍然会丢失,或者您应该将其添加到类中。我已经删去了大量代码,以尝试简化它tbf mate,但谢谢您,如果Tseng的答案不正确,我将尝试work@JoelHarkes我需要什么包来访问[Xml]mate?我需要安装什么程序包才能获得这些名称空间朋友?我已将程序包添加到System.Runtime.Serialization.Xml,将您的属性添加到“我的类”的顶部,但我没有得到任何输出。DataMember和DataContract属性也没有任何效果:/Microsoft安装了一个名为packagesearch的精彩网站,您可以通过按类型搜索来执行反向包搜索:。CollectionDataContractAttribute位于System.Runtime.Serialization.Primitives包中,至少版本为4.1.1。当我安装所述包并在模型上添加所述属性时,我会得到“get net::ERR\u CONNECTION\u RESET”和一个空白页?