【问题标题】:ASP.NET Core How to output custom XML ModelASP.NET Core 如何输出自定义 XML 模型
【发布时间】:2017-03-15 15:04:08
【问题描述】:

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

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

        // 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>

如何在不破坏接受标头 JSON + XML 输入/输出功能的情况下更改我的 XML 输出以匹配上述内容?

【问题讨论】:

  • 您可以使用[Xml...Atribute] 更改标签的名称,尽管&lt;Postcode/&gt; 当然仍然会丢失,或者您应该将其添加到您的课程中。
  • 我已经剪掉了很多代码来尝试简化它tbf mate,但是谢谢你,如果Tseng的答案不起作用,我会尝试一下
  • @JoelHarkes 访问 [Xml] mate 需要什么包?

标签: c# xml asp.net-core formatting


【解决方案1】:

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

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

【讨论】:

  • 我需要安装什么包来获得这些命名空间的朋友?
  • 我已将包添加到 System.Runtime.Serialization.Xml,将您的属性添加到我的班级顶部,但我根本没有得到任何输出。
  • DataMember 和 DataContract 属性也没有效果:/
  • 微软设置了这个名为“packagesearch”的精彩站点,您可以在其中通过搜索类型进行反向包搜索:packagesearch.azurewebsites.net/…。 CollectionDataContractAttribute 在 System.Runtime.Serialization.Primitives 包中,至少版本 4.1.1
  • 当我安装所述软件包并在我的模型上添加所述属性时,我得到 'GET localhost:52903/api/AcceptedInvoices net::ERR_CONNECTION_RESET' 和一个空白页?
猜你喜欢
  • 2018-12-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-07
相关资源
最近更新 更多