net核心的返回xml中删除xmlns

net核心的返回xml中删除xmlns

本文介绍了如何在.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>

然后它将返回以下内容:

Then it will return these:

<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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 21:23