Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.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# 如何删除.net核心中返回的xml中的xmlns:xsi和xmlns:xsd?_C#_Asp.net Core_.net Core - Fatal编程技术网

C# 如何删除.net核心中返回的xml中的xmlns:xsi和xmlns:xsd?

C# 如何删除.net核心中返回的xml中的xmlns:xsi和xmlns:xsd?,c#,asp.net-core,.net-core,C#,Asp.net Core,.net Core,这是我的密码: [HttpPost] [Produces("application/xml")] public async Task<xml> mp([FromBody]xml XmlData) { xml ReturnXmlData = null; ReturnXmlData = new xml() { ToUserName = XmlData.FromUserName, FromUserName = XmlData.ToUs

这是我的密码:

[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; }
}
[HttpPost]
[产生(“应用程序/xml”)]
公共异步任务mp([FromBody]XmlData)
{
xml ReturnXmlData=null;
ReturnXmlData=newXML()
{
ToUserName=XmlData.FromUserName,
FromUserName=XmlData.ToUserName,
CreateTime=XmlData.CreateTime,
MsgType=“text”,
Content=“你好,世界”
};
返回XML数据;
}
[XmlRoot(“xml”)]
公共类xml
{
公共字符串ToUserName{get;set;}
来自用户名{get;set;}的公共字符串
公共字符串CreateTime{get;set;}
公共字符串MsgType{get;set;}
公共字符串MsgId{get;set;}
公共字符串内容{get;set;}
}
现在,在我将这些代码发布到用于测试的本地服务器之后:

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

123
45
12345678
文本
变灰
然后它将返回这些:

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

45
123
20190921203758
文本
你好,世界
嗯,正如你所看到的。XML数据包含远程服务器中不允许的xmlns:xsi和xmlns:xsd

此外,远程服务器不是由我们控制的,我不能用它更改任何代码或规则

这意味着我必须修改返回XML,如下所示:

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

45
123
20190921203758
文本
你好,世界

当返回XML时,如何删除xmlns:xsi和xmlns:xsd?谢谢。

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

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()