使用ASP.NET Core时如何删除xml根名称 [英] How to remove xml root namesapce when using asp.net core

查看:48
本文介绍了使用ASP.NET Core时如何删除xml根名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

鉴于我正在使用asp.net核心和XmlDataContractSerializerOutputFormatter格式化响应,因此如何删除根xml命名空间.

How can i remove the root xml namespace given that I am using asp.net core and the XmlDataContractSerializerOutputFormatter to format the response.

所有返回的xml文档具有以下格式

All of the returned xml docs have the following format

<?xml version="1.0" encoding="utf-8"?>
   <response xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
      some other stuff
   </response>

我需要删除xmlns:i ="http://www.w3.org/2001/XMLSchema-instance"部分.

I need to remove the xmlns:i="http://www.w3.org/2001/XMLSchema-instance" part.

推荐答案

我最终扩展了现有的XmlDataContractSerializerOutputFormatter,使用了创建xmlwriter对象的方法并将其包装在适配器中以编辑功能,因为编辑响应不是由于该框架所代表的响应对象的结构,这并不是一个切实可行的解决方案.

I ended up extending the existing XmlDataContractSerializerOutputFormatter, tapping into the method that creates the xmlwriter object and wrapping it in an adapter to edit the functionality, since editing the response wasn't really a practical nor feasible solution due to the structure of the response object as represented by the framework.

首先是格式化程序

public class CustomDataContractOutputFormatter : XmlDataContractSerializerOutputFormatter
{
    public CustomDataContractOutputFormatter() : base(){}
    public CustomDataContractOutputFormatter(XmlWriterSettings set) : base(set){}
    public override XmlWriter CreateXmlWriter(TextWriter writer, XmlWriterSettings xmlWriterSettings)
    {
        var bas = base.CreateXmlWriter(writer, xmlWriterSettings);
        var ret = new XmlWriterNoNamespace();
        ret.AdaptedWriter = bas;
        return ret;
    }
}

然后连接基本适配器(部分适配器)

Then the base adapter (part of it)

public class XmlWriterAdapter : XmlWriter
{
    XmlWriter _adaptedWriter;
    public XmlWriterAdapter():base(){}
    public XmlWriter AdaptedWriter 
    {
        get
        {
            return _adaptedWriter;
        } 
        set
        {
            this._adaptedWriter = value;
        }
    }
    public override WriteState WriteState { get{return _adaptedWriter.WriteState;} }
    public override void Flush()
    {
        _adaptedWriter.Flush();
    }
    public override string LookupPrefix(string ns)
    {
        return _adaptedWriter.LookupPrefix(ns);
    }
}

最后是带有破解功能的特定适配器,以禁止写入名称空间

And finally the specific adapter with a hack to disable writing of the namespace

public class XmlWriterNoNamespace : XmlWriterAdapter
{
    bool _skipAttribute;
    public XmlWriterNoNamespace():base(){}
    public override void WriteEndAttribute()
    {
        if(_skipAttribute)
        {
            _skipAttribute = false;
            return;
        }
        base.WriteEndAttribute();
    }
    public override void WriteStartAttribute(string prefix, string localName, string ns)
    {
        if(prefix.Equals("xmlns"))
        {
            _skipAttribute = true;
            return;
        }
        base.WriteStartAttribute(prefix, localName, ns);
    }
    public override void WriteString(string text)
    {
        if(_skipAttribute)
            return;
        base.WriteString(text);
    }
} 

这篇关于使用ASP.NET Core时如何删除xml根名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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