自定义xmlWriter跳过某些元素? [英] Custom xmlWriter to skip a certain element?

查看:96
本文介绍了自定义xmlWriter跳过某些元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在实现自定义xmlwriter,我想省略任何具有特定名称的元素,例如< ABC>

I am implementing a custom xmlwriter and I want to omit any elements with a particular name, like <ABC>.

实际上我该怎么做?我觉得我可能需要像这样覆盖 WriteStartElement 方法:

How do I actually do that? I get a sense that I might need to overwrite the WriteStartElement method like this:

public override void WriteStartElement(string prefix, string localName, string ns)
{

    if (localName.Equals("ABC")) 
    {
        return;
    }
    base.WriteStartElement(prefix, localName, ns);
}

我还需要覆盖 WriteEndElement 方法?如何告诉 WriteEndElement 方法跳过写结尾的< / ABC> 标记?我可以检查的 WriteEndElement 方法中似乎没有对 localName 的引用。?

Do I also need to overwrite WriteEndElement method? How do I tell WriteEndElement method to skip writing the end </ABC> tag? There seems to be no reference of the localName in WriteEndElement method that I can check...?

推荐答案

在我之前编写的一些示例代码中找到了这一点。它维护一个下推堆栈,以确定是否要写入元素结尾,这是您需要做的:

Found this in some sample code I wrote previously. It maintains a push-down stack to determine whether to write the element end, which is what you would need to do:

public class ElementSkippingXmlTextWriter : XmlWriterProxy
{
    readonly Stack<bool> stack = new Stack<bool>();
    readonly Func<string, string, int, bool> filter;
    readonly Func<string, string, int, string> nameEditor;
    readonly bool filterChildren;

    public ElementSkippingXmlTextWriter(XmlWriter writer, Func<string, string, int, bool> filter, bool filterChildren)
        : this(writer, filter, null, filterChildren)
    {
    }

    public ElementSkippingXmlTextWriter(XmlWriter writer, Func<string, string, int, bool> filter, Func<string, string, int, string> nameEditor, bool filterChildren)
        : base(writer)
    {
        this.filter = filter ?? delegate { return true; };
        this.nameEditor = nameEditor ?? delegate(string localName, string ns, int depth) { return localName; };
        this.filterChildren = filterChildren;
    }

    protected override bool IsSuspended
    {
        get
        {
            if (filterChildren)
            {
                if (!stack.All(b => b))
                    return true;
            }
            else
            {
                if (stack.Count > 0 && !stack.Peek())
                    return true;
            }

            return base.IsSuspended;
        }
    }

    public override void WriteStartElement(string prefix, string localName, string ns)
    {
        var write = filter(localName, ns, stack.Count);
        var newLocalName = nameEditor(localName, ns, stack.Count);
        if (write)
            base.WriteStartElement(prefix, newLocalName, ns);
        stack.Push(write);
    }

    public override void WriteEndElement()
    {
        if (stack.Pop())
            base.WriteEndElement();
    }
}

public class XmlWriterProxy : XmlWriter
{
    readonly XmlWriter baseWriter;

    public XmlWriterProxy(XmlWriter baseWriter)
    {
        if (baseWriter == null)
            throw new ArgumentNullException();
        this.baseWriter = baseWriter;
    }

    protected virtual bool IsSuspended { get { return false; } }

    public override void Close()
    {
        baseWriter.Close();
    }

    public override void Flush()
    {
        baseWriter.Flush();
    }

    public override string LookupPrefix(string ns)
    {
        return baseWriter.LookupPrefix(ns);
    }

    public override void WriteBase64(byte[] buffer, int index, int count)
    {
        if (IsSuspended)
            return;
        baseWriter.WriteBase64(buffer, index, count);
    }

    public override void WriteCData(string text)
    {
        if (IsSuspended)
            return;
        baseWriter.WriteCData(text);
    }

    public override void WriteCharEntity(char ch)
    {
        if (IsSuspended)
            return;
        baseWriter.WriteCharEntity(ch);
    }

    public override void WriteChars(char[] buffer, int index, int count)
    {
        if (IsSuspended)
            return;
        baseWriter.WriteChars(buffer, index, count);
    }

    public override void WriteComment(string text)
    {
        if (IsSuspended)
            return;
        baseWriter.WriteComment(text);
    }

    public override void WriteDocType(string name, string pubid, string sysid, string subset)
    {
        if (IsSuspended)
            return;
        baseWriter.WriteDocType(name, pubid, sysid, subset);
    }

    public override void WriteEndAttribute()
    {
        if (IsSuspended)
            return;
        baseWriter.WriteEndAttribute();
    }

    public override void WriteEndDocument()
    {
        if (IsSuspended)
            return;
        baseWriter.WriteEndDocument();
    }

    public override void WriteEndElement()
    {
        if (IsSuspended)
            return;
        baseWriter.WriteEndElement();
    }

    public override void WriteEntityRef(string name)
    {
        if (IsSuspended)
            return;
        baseWriter.WriteEntityRef(name);
    }

    public override void WriteFullEndElement()
    {
        if (IsSuspended)
            return;
        baseWriter.WriteFullEndElement();
    }

    public override void WriteProcessingInstruction(string name, string text)
    {
        if (IsSuspended)
            return;
        baseWriter.WriteProcessingInstruction(name, text);
    }

    public override void WriteRaw(string data)
    {
        if (IsSuspended)
            return;
        baseWriter.WriteRaw(data);
    }

    public override void WriteRaw(char[] buffer, int index, int count)
    {
        if (IsSuspended)
            return;
        baseWriter.WriteRaw(buffer, index, count);
    }

    public override void WriteStartAttribute(string prefix, string localName, string ns)
    {
        if (IsSuspended)
            return;
        baseWriter.WriteStartAttribute(prefix, localName, ns);
    }

    public override void WriteStartDocument(bool standalone)
    {
        baseWriter.WriteStartDocument(standalone);
    }

    public override void WriteStartDocument()
    {
        baseWriter.WriteStartDocument();
    }

    public override void WriteStartElement(string prefix, string localName, string ns)
    {
        if (IsSuspended)
            return;
        baseWriter.WriteStartElement(prefix, localName, ns);
    }

    public override WriteState WriteState
    {
        get { return baseWriter.WriteState; }
    }

    public override void WriteString(string text)
    {
        if (IsSuspended)
            return;
        baseWriter.WriteString(text);
    }

    public override void WriteSurrogateCharEntity(char lowChar, char highChar)
    {
        if (IsSuspended)
            return;
        baseWriter.WriteSurrogateCharEntity(lowChar, highChar);
    }

    public override void WriteWhitespace(string ws)
    {
        if (IsSuspended)
            return;
        baseWriter.WriteWhitespace(ws);
    }
}

它包装了 XmlWriter ,您将像往常一样通过调用 XmlWriter.Create() ,并过滤<$ c对应的元素(以及子元素) $ c> filter 委托返回 false

It wraps an XmlWriter you would create as usual with a call to XmlWriter.Create(), and filters elements (and, optionally, children) for which the filter delegate returns false.

注意-未经全面测试。 异步方法也可能需要被覆盖。

Note -- not fully tested. Async methods may need to be overridden as well.

这篇关于自定义xmlWriter跳过某些元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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