Created
January 14, 2015 12:09
-
-
Save hanswolff/37ebc91413e7c0cfc600 to your computer and use it in GitHub Desktop.
Wrapped XmlWriter that can remove blank attributes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Xml; | |
/// <summary> | |
/// Wrapped XmlWriter that can remove blank attributes | |
/// </summary> | |
public class CustomizableXmlWriter : XmlWriter | |
{ | |
private readonly XmlWriter _baseWriter; | |
private readonly Queue<Tuple<string, Action>> _retainedActions = new Queue<Tuple<string, Action>>(); | |
public bool SkipBlankAttributes { get; set; } | |
public CustomizableXmlWriter(XmlWriter baseWriter) | |
{ | |
if (baseWriter == null) throw new ArgumentNullException("baseWriter"); | |
_baseWriter = baseWriter; | |
} | |
public override void WriteStartDocument() | |
{ | |
FlushRetainedActions(); | |
_baseWriter.WriteStartDocument(); | |
} | |
public override void WriteStartDocument(bool standalone) | |
{ | |
FlushRetainedActions(); | |
_baseWriter.WriteStartDocument(standalone); | |
} | |
public override void WriteEndDocument() | |
{ | |
FlushRetainedActions(); | |
_baseWriter.WriteEndDocument(); | |
} | |
public override void WriteDocType(string name, string pubid, string sysid, string subset) | |
{ | |
FlushRetainedActions(); | |
_baseWriter.WriteDocType(name, pubid, sysid, subset); | |
} | |
public override void WriteStartElement(string prefix, string localName, string ns) | |
{ | |
FlushRetainedActions(); | |
_baseWriter.WriteStartElement(prefix, localName, ns); | |
} | |
public override void WriteEndElement() | |
{ | |
FlushRetainedActions(); | |
_baseWriter.WriteEndElement(); | |
} | |
public override void WriteFullEndElement() | |
{ | |
FlushRetainedActions(); | |
_baseWriter.WriteFullEndElement(); | |
} | |
public override void WriteStartAttribute(string prefix, string localName, string ns) | |
{ | |
_retainedActions.Enqueue(new Tuple<string, Action>("WriteStartAttribute", () => _baseWriter.WriteStartAttribute(prefix, localName, ns))); | |
} | |
public override void WriteEndAttribute() | |
{ | |
if (SkipBlankAttributes) | |
{ | |
var hasWriteString = _retainedActions.Any(x => x.Item1 == "WriteString"); | |
if (!hasWriteString && _retainedActions.Peek().Item1 == "WriteStartAttribute") | |
{ | |
_retainedActions.Dequeue(); | |
return; | |
} | |
} | |
FlushRetainedActions(); | |
_baseWriter.WriteEndAttribute(); | |
} | |
private void FlushRetainedActions() | |
{ | |
while (_retainedActions.Count > 0) | |
{ | |
var tuple = _retainedActions.Dequeue(); | |
tuple.Item2(); | |
} | |
} | |
public override void WriteCData(string text) | |
{ | |
FlushRetainedActions(); | |
_baseWriter.WriteCData(text); | |
} | |
public override void WriteComment(string text) | |
{ | |
FlushRetainedActions(); | |
_baseWriter.WriteComment(text); | |
} | |
public override void WriteProcessingInstruction(string name, string text) | |
{ | |
FlushRetainedActions(); | |
_baseWriter.WriteProcessingInstruction(name, text); | |
} | |
public override void WriteEntityRef(string name) | |
{ | |
FlushRetainedActions(); | |
_baseWriter.WriteEntityRef(name); | |
} | |
public override void WriteCharEntity(char ch) | |
{ | |
FlushRetainedActions(); | |
_baseWriter.WriteCharEntity(ch); | |
} | |
public override void WriteWhitespace(string ws) | |
{ | |
FlushRetainedActions(); | |
_baseWriter.WriteWhitespace(ws); | |
} | |
public override void WriteString(string text) | |
{ | |
if (SkipBlankAttributes) | |
{ | |
if (text == "" && _retainedActions.Any() && _retainedActions.Peek().Item1 == "WriteStartAttribute") | |
{ | |
return; | |
} | |
} | |
FlushRetainedActions(); | |
_retainedActions.Enqueue(new Tuple<string, Action>("WriteString", () => _baseWriter.WriteString(text))); | |
} | |
public override void WriteSurrogateCharEntity(char lowChar, char highChar) | |
{ | |
FlushRetainedActions(); | |
_baseWriter.WriteSurrogateCharEntity(lowChar, highChar); | |
} | |
public override void WriteChars(char[] buffer, int index, int count) | |
{ | |
FlushRetainedActions(); | |
_baseWriter.WriteChars(buffer, index, count); | |
} | |
public override void WriteRaw(char[] buffer, int index, int count) | |
{ | |
FlushRetainedActions(); | |
_baseWriter.WriteRaw(buffer, index, count); | |
} | |
public override void WriteRaw(string data) | |
{ | |
FlushRetainedActions(); | |
_baseWriter.WriteRaw(data); | |
} | |
public override void WriteBase64(byte[] buffer, int index, int count) | |
{ | |
FlushRetainedActions(); | |
_baseWriter.WriteBase64(buffer, index, count); | |
} | |
public override void Flush() | |
{ | |
FlushRetainedActions(); | |
_baseWriter.Flush(); | |
} | |
public override string LookupPrefix(string ns) | |
{ | |
return _baseWriter.LookupPrefix(ns); | |
} | |
public override WriteState WriteState | |
{ | |
get { return _baseWriter.WriteState; } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment