1

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

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);
}

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

Broots Waymb
  • 4,713
  • 3
  • 28
  • 51
Jason
  • 821
  • 2
  • 16
  • 28
  • It may help answering your question if you provide some context. Why do you want to do this? – Krumelur Aug 21 '15 at 21:46
  • 3
    Why don't you try and see for yourself? – BartoszKP Aug 21 '15 at 21:50
  • @Krumelur Because that tag carries useless content and will be confusing to show. – Jason Aug 21 '15 at 21:50
  • @BartoszKP I would. And I will definitely try out the WriteStartElement part. Just thought I should also overwrite WriteEndElement somehow to skip the ending tag but I am not sure how. – Jason Aug 21 '15 at 21:52
  • Do you need its children in the output? – Krumelur Aug 21 '15 at 21:52
  • You need to match WriteStart with a WriteEnd, meaning you need to count the number of start elements skipped – Krumelur Aug 21 '15 at 21:53
  • @Krumelur mmmm... That seems to be a bit more complex than I thought. This tag won't have any nested child. Is there any good way to just skip it without having to count the numbers of start elements skipped? – Jason Aug 21 '15 at 22:12
  • 3
    There are lots of possible ways to address the basic need, making this question far too broad for StackOverflow. That said, I will recommend that your best bet would be to use LINQ and the System.Xml.Linq namespace objects. System.Xml.Linq provides an easy, LINQ-compatible API for manipulating the XML DOM, and of course LINQ itself provides great filtering and other query-like constructs. – Peter Duniho Aug 21 '15 at 22:14
  • Maybe you just should to remove nodes "ABC" from `XmlDocument`? – Alexander Petrov Aug 21 '15 at 22:14
  • Do you want to eliminate just the `"ABC"` node and propagate all children to the parent node, or do you want to eliminate the `"ABC"` node and all children? – dbc Aug 22 '15 at 00:33

1 Answers1

2

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 WriteState WriteState { get { return baseWriter.WriteState; } }
    public override XmlWriterSettings Settings { get { return baseWriter.Settings; } }
    public override XmlSpace XmlSpace { get { return baseWriter.XmlSpace; } }
    public override string XmlLang { get { return baseWriter.XmlLang; } }

    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 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);
    }
}

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.

dbc
  • 104,963
  • 20
  • 228
  • 340