What's new in System.Xml 2.0

Working with XML Schemas

This article was originally published on DNJ Online
DNJ Online
This article was originally published on DNJ Online
DNJ

As you saw in the previous section, validating XML documents is easier in version 2.0 because the XmlReader and XmlDocument classes now support this directly. However, creating schemas for your XML documents is not a trivial task.

In version 2.0, you can infer XML schemas from existing XML documents using the new XmlSchemaInference class. There are options that define how restrictive the schema is, and a new class named XmlSchemaSet which correctly handles multiple schemas that contain the same namespace definitions (replacing the XmlSchemaCollection class in version 1.x). For example, the XML shown below can be used to infer a schema for this document.

<?xml version="1.0" encoding="utf-8"?>
<root xmlns="http://myns/inference-demo">
<session name="All about XML">
<slides>
<slide position="1">
<title>Agenda</title>
<reviewed>2004-05-10T00:00:00</reviewed>
</slide>
<slide position="2">
<title>Introduction</title>
<reviewed>2003-10-22T00:00:00</reviewed>
</slide>
<slide position="3">
<title>Code Example</title>
<reviewed>2004-03-02T00:00:00</reviewed>
</slide>
</slides>
</session>
</root>

Here is the result of a simple routine that infers a schema for this document using the XmlSchemInference class, and writes it to the screen and a disk file:

You can also infer schemas, or create them from scratch, using the tools in Visual Studio 2005, as described in the article that covers Data and XML support in Visual Studio 2005.

Performing XSLT Transformations

One embarrassing feature of .NET Framework 1.x is that XSLT transformation engine exhibits extremely poor performance when compared to the version 4 release of the standard MSXML parser. This is partly because the new version of MSXML compiles the style sheets is uses. In version 2.0 of the .NET Framework, Microsoft has introduced a new XSLT transformation engine that provides two to four times better performance than the XsltTransform class in version 1.x. The new XslCompiledTransform class compiles style sheets to MSIL code before executing them, which also solves the problems with malformed style sheets encountered in the XslTransform class.

XML Serialization Enhancements

One important feature when working with XML is the ability to serialize it from classes that expose it, or de-serialize it for classes that consume it. While, in most cases, you don't have to get your hands dirty and worry about the details, you can now build your own serialization routines much more easily using the new features of the XmlSerializer in version 2.0. Pre-generation of serialization assemblies is supported, together with methods for encoding schemas, type mapping, and exposing events. There is also enhanced support for Web Service serialization.

XML Security Enhancements

Like any other technology that you run on your machines, XML can expose your applications to errant or intentionally malicious code. This is particularly the case with XSLT style sheets which can contain script and even reference compiled code assemblies. In version 2.0, the classes in the System.Xml and the subsidiary namespaces have improved protection against such attacks.

For example, the default settings for the XmlReader and XmlDocument classes prevent denial of service attaches from DTDs embedded in XML documents, and the XslCompiledTransform class disables the document function and prevents scripts from running in a style sheet by default. Plus, the XmlSecureResolver class can be used to prevent access to resources that are restricted through policies on a machine.

You might also like...

Comments

About the author

Alex Homer

Alex Homer United Kingdom

Alex spent most of his earlier working life as a technical salesman, and has had a love-hate relationship with computers that goes way back to the Sinclair Z80 and the Oric Atmos. In 1996 he ret...

Interested in writing for us? Find out more.

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.”