We've been doing some fun stuff with XML lately, you might have noticed. We've used:
Also. we're using:
class KindOfPartialSgmlButMoreOfAnOfxXmlWriter : XmlWriter
{
public KindOfPartialSgmlButMoreOfAnOfxXmlWriter(TextWriter w)
{
_htmlWriter = new HtmlTextWriter(w);
}
public override void Close()
{
_htmlWriter.Close();
}
private Stack _tags = new Stack();
private HtmlTextWriter _htmlWriter = null;
private bool _suppressNextEndElement = false;
private bool _suppressNextText = false;
private string _previousElement = null;
public override void WriteStartElement(string prefix, string localName, string ns)
{
_htmlWriter.WriteFullBeginTag(localName);
_previousElement = localName;
_tags.Push(localName);
}
public override void WriteString(string text)
{
if (_suppressNextText == false)
{
_htmlWriter.Write(text);
_suppressNextEndElement = true;
}
}
public override void WriteEndElement()
{
string endtag = _tags.Pop() as string;
if (_suppressNextEndElement && endtag == _previousElement)
{
_suppressNextEndElement = false;
}
else
{
_htmlWriter.WriteEndTag(endtag);
}
}
public override void Flush()
{
_htmlWriter.Flush();
}
public override void WriteWhitespace(string ws)
{
_htmlWriter.Write(ws);
}
public override void WriteRaw(string data)
{
_htmlWriter.Write(data);
}
public override void WriteChars(char[] buffer, int index, int count)
{
_htmlWriter.Write(buffer, index, count);
}
public override void WriteQualifiedName(string localName, string ns)
{
_htmlWriter.WriteBeginTag(localName);
}
public override void WriteEndAttribute()
{
_suppressNextText = false;
}
public override void WriteStartAttribute(string prefix, string localName, string ns)
{
_suppressNextText = true;
}
public override void WriteRaw(char[] buffer, int index, int count)
{
_htmlWriter.Write(buffer,index,count);
}
public override void WriteProcessingInstruction(string name, string text)
{
_htmlWriter.Write(text);
}
#region Stubs
...here are overriden versions of everything else from XmlWriter, stubbed and not used. Removed for tidiness
#endregion
}