Jesper Tverskov, June 24, 2008

XML APIs in ASP.NET

LINQ to XML, an "all-in-one" API, can create XML from scratch better than XmlWriter, read and edit it better than W3C DOM and we can even transform it like XSLT and query it like XQuery. Only sad that LINQ to XML is a proprietary Microsoft API not a standard.

The strength of an all-in-one XML API based on a general API for all types of data, LINQ, is so great, that LINQ to XML is bound to replace XmlWriter, W3C DOM, XQuery and even XSLT for very many use cases in varying degrees.

We still need XmlReader, a forward only reader, for Validation and to optimize for speed, and the XSLT like transformations in LINQ to XML are only child's play compared to hardcore XSLT using templates and recursion.

1. Validate XML

2. Query XML

3. Transform XML

4. Edit XML

LINQ to XML is easier to use than W3C DOM. But W3C DOM is a standard API implemented in almost any programming language.

5. Read XML

In ASP.NET 1.0 we basically had two alternatives: the XmlDocument class (W3C DOM) working on the XML hierarchy in memory (both for reading and editing), and the fast, forward looking, SAX-like, read-only, XmlReader class.

In ASP.NET 2.0. we got the XPathDocument class also working on the XML hierarchy in memory, but much faster than XmlDocument and replacing it when editing is not needed. We now also have LINQ to XML working on the hierarchy in memory, both for editing and reading, replacing both XPathDocument and XmlDocument for most use cases.

6. Create XML

In ASP.NET we have three main APIs for creating XML from scratch: LINQ to XML, W3C DOM and XmlWriter. LINQ to XML is really all we need.

Updated 2009-08-07