XmlTextWriter Can Produce Invalid XML
XML / XPath / XSLT csharp xml
Published: 2007-06-16
XmlTextWriter Can Produce Invalid XML

XmlTextWriter is .NET’s class for writing XML in a forward-only streaming manner. It is highly efficient and is the preferred way to generate XML in .NET in most circumstances. I find XmlTextWriter so useful I wrote a partial C++ implementation of it in Implenting IXmlWriter Series.

Unfortunately, XmlTextWriter isn’t quite as strict as it could be. It will let slip some invalid XML such as duplicate attributes, invalid Unicode characters in the range 0×0 to 0×20, and invalid element and attribute names. You can read about XmlTextWriter’s limitations in the article Customized XML Writer Creation.

If these limitations are an issue for you, I suggest following the instructions in “Customized XML Writer Creation” by writing a custom writer that extends the current XmlTextWriter and adds this functionality. This class can be used directly or passed to any functions which are designed to use XmlTextWriter.