XmlTextWriter Can Produce Invalid XML

C# Add comments

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.

One Response to “XmlTextWriter Can Produce Invalid XML”

  1. Saul Pwanson Says:

    The C library libxml2 will also output invalid XML, which it will then fail to parse. We found out that you can create elements with numeric (invalid) tag names, and attributes with unquoted invalid characters.

Leave a Reply

WP Theme & Icons by N.Design Studio
Entries RSS Comments RSS Log in