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.
I’m not sure if this is the “canonical” way to do it but here’s a description of how to write an ASP.NET 1.1 ASPX page which returns a XML document (e.g. when writing a home-brewed web service). First, create a new Web Form (I will call it WebService.aspx). As we will be progamatically generating the XML in the HTTP response rather than sending the (processed) content of the ASPX file, delete everything from the ASPX file but the @Page directive, so that it looks something like:
This is part 14 of my Implementing IXmlWriter post series. Today I will add support for writing the generated XML to a C++ stream to last time’s IXmlWriter. Finally the reason why I’ve insisted on calling this series IXmlWriter (instead of StringXmlWriter) should become clear: I’ve been planning on supporting writing the generated XML to more than just a string. Specifically, today I will add the ability to write the XML to a C++ ostream object, a base class in the C++ iostream library which defines a writable stream.
This is part 13 of my Implementing IXmlWriter post series. As the private members of IXmlWriter are getting too numerous and too likely to change by my judgment, today I will put last time’s IXmlWriter behind a compilation firewall (pimpl). The idea behind the pimpl idiom is to hide as much of the class definition as possible in order to avoid requiring users of the class to recompile if the class’s private members are changed.
This is part 12 of my Implementing IXmlWriter post series. Today I will add support for pretty-printing to last time’s IXmlWriter. Pretty-printing is the addition of whitespace at predetermined locations to make the resulting XML easier to read than when it is all on one line. In the .NET Framework’s System.Xml.XmlTextWriter class, it is supported by the properties Formatting, which allows you to enable or disable pretty-printing; Indentation, which allows you to specify how many whitespace characters indentation should use; and IndentChar, which allows you to specify the whitespace character to use for indentation.
This is part 11 of my Implementing IXmlWriter post series. Today I will add support for namespaces to last time’s IXmlWriter. Namespaces are defined by the W3C recommendation Namespaces in XML. Using namespaces requires two parts: a namespace declaration, which associates a prefix with a namespace name (a user-defined, ideally globally-unique string which defines the namespace, often in the form of a URL); and the assignment of XML elements and attributes to this namespace by using the aforementioned prefix.
This is part 10 of my Implementing IXmlWriter post series. Today I will add support for the function WriteComment() to last time’s IXmlWriter. Quoting from Section 2.5: Comments of the XML 1.0 spec: Comments MAY appear anywhere in a document outside other markup; in addition, they MAY appear within the document type declaration at places allowed by the grammar. Considering this, we should allow writing comments in virtually every WriteState that the IXmlWriter can be in.
This is part 9 of my Implementing IXmlWriter post series. Today I will add support for the functions WriteStartDocument() and WriteEndDocument() to last time’s IXmlWriter. WriteStartDocument() writes the XML declaration (i.e. <?xml version="1.0"?>) and WriteEndDocument() closes all open attributes and elements and sets the IXmlWriter back in the initial state. Adding support for these functions is straightforward. Note that I have introduced a new IXmlWriter state called WriteState_Prolog; this will be important later.
This is part 8 of my Implementing IXmlWriter post series. Today I will add support for the functions WriteStartAttribute() and WriteEndAttribute() to last time’s IXmlWriter. These functions are (obviously) used to denote the start and end of an attribute; the attribute value is written using WriteString() (this usage is analogous to WriteStartElement() and WriteEndElement()). Because WriteString() must now be aware of whether it is writing an attribute value or element content, I must keep track of the state the IXmlWriter is in — a change that affects nearly every function.
This is part 7 of my Implementing IXmlWriter post series. Wow, I can’t believe that it’s been over a month already since my last IXmlWriter post. I guess my vacation ruined my exercise plan and my blogging habits. It’s well past time to get back into both. Rather than introduce a new test case, I’m going to spend today “cleaning up” the previous version of IXmlWriter. The first cleanup method is trivial but overdue — I will separate the implementation of IXmlWriter from its interface, as a user of IXmlWriter shouldn’t be particularly concerned about its implementation.