如何在 .net 核心的返回 xml 中删除 xmlns:xsi 和 xmlns:xsd? [英] How can I remove xmlns:xsi and xmlns:xsd in the return xml in .net core?

查看:34
本文介绍了如何在 .net 核心的返回 xml 中删除 xmlns:xsi 和 xmlns:xsd?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码:

[HttpPost]
[Produces("application/xml")]
public async Task<xml> mp([FromBody]xml XmlData)
{
    xml ReturnXmlData = null;
    ReturnXmlData = new xml()
    {
        ToUserName = XmlData.FromUserName,
        FromUserName = XmlData.ToUserName,
        CreateTime = XmlData.CreateTime,
        MsgType = "text",
        Content = "Hello world"
    };
    return ReturnXmlData;
}
[XmlRoot("xml")]
public class xml
{
    public string ToUserName { get; set; }
    public string FromUserName { get; set; }
    public string CreateTime { get; set; }
    public string MsgType { get; set; }
    public string MsgId { get; set; }
    public string Content { get; set; }
}

现在我将这些代码发布到本地服务器进行测试后:

Now after I post these code to the local server which for test:

<xml>
  <ToUserName>123</ToUserName>
  <FromUserName>45</FromUserName>
  <CreateTime>12345678</CreateTime>
  <MsgType>text</MsgType>
  <Content>greating</Content>
</xml>

然后它会返回这些:

<xml xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <ToUserName>45</ToUserName>
  <FromUserName>123</FromUserName>
  <CreateTime>20190921203758</CreateTime>
  <MsgType>text</MsgType>
  <Content>Hello world</Content>
</xml>

好吧,如你所见.XML 数据包含远程服务器中不允许的 xmlns:xsi 和 xmlns:xsd.

Well, as you see. The XML data contains xmlns:xsi and xmlns:xsd which are not allowed in the remote server.

另外,远程服务器不受我们控制,我不能用它更改任何代码或任何规则.

In addition, the remote server is not control by us that I can't change any code or any rules with it.

这意味着我必须像这样修改返回的 XML:

That means I have to modify the return XML like this:

<xml>
  <ToUserName>45</ToUserName>
  <FromUserName>123</FromUserName>
  <CreateTime>20190921203758</CreateTime>
  <MsgType>text</MsgType>
  <Content>Hello world</Content>
</xml>

如何在返回 XML 时删除 xmlns:xsi 和 xmlns:xsd?谢谢.

How can I remove the xmlns:xsi and xmlns:xsd when returns the XML? Thank you.

推荐答案

您可以为 xml 创建自定义序列化程序格式化程序,您可以从默认的 XmlSerializerOutputFormatter 实现中继承它

You can create custom serializer formatter for xml and you can inherit it from default XmlSerializerOutputFormatter implementation

public class XmlSerializerOutputFormatterNamespace : XmlSerializerOutputFormatter
{
    protected override void Serialize(XmlSerializer xmlSerializer, XmlWriter xmlWriter, object value)
    {
        //applying "empty" namespace will produce no namespaces
        var emptyNamespaces = new XmlSerializerNamespaces();
        emptyNamespaces.Add("", "any-non-empty-string");
        xmlSerializer.Serialize(xmlWriter, value, emptyNamespaces);
    }
}

Startup

services
    .AddMvc(options =>
    {
        options.OutputFormatters.Add(new XmlSerializerOutputFormatterNamespace());
    })
    //there should be one of the following lines in your application already in order to make xml serialization work
    //our custom output formatter will override default one since it's iterated earlier in OutputFormatters collection
    .AddXmlSerializerFormatters()
    //.AddXmlDataContractSerializerFormatters()

这篇关于如何在 .net 核心的返回 xml 中删除 xmlns:xsi 和 xmlns:xsd?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆