Send a suggestion!

We're building a brand new version of the site, and we'd love to hear your ideas

Members

Technology Zones

IBM Learning Center

Articles

Hosted By

MaximumASP

Info

Rated
Read 19,740 times

Contents

Related Categories

What's new in System.Xml 2.0 - Working with XML Schemas

alexhomer

Working with XML Schemas

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.

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 retired from full-time work to concentrate on writing and developing in the fast-growing arena of the Web. This coincided with the release of Microsoft ASP 1.0, and since then he has been involved almost exclusively in that area.

Alex has written or contributed to over 40 books on Web technologies for Wrox Press, Sams Publishing, APress! and Addison Wesley, predominantly on Microsoft ASP and ASP.NET, XML, and mobile device support. He has also contributed more than 20 articles to ASPToday, plus several to other ASP-related Web resources sites such as DevX and 15seconds.com.

He has presented sessions about Web technologies at four Wrox conferences, two ASPDevCon conferences, as well as Microsoft's PDC, Tech-Ed, ASP.NET Connections and VS-Live conferences. He has also conducted training sessions for McKesson HBOC, BMS Limited, and other local companies and developers on ASP.NET and XML. In addition he has provided talks and demonstrations for local Dot Net User Groups, DDG-UK, and even for user groups in exotic locations such as Vienna.

Alex has been awarded MVP status by Microsoft, and is also a member of the CodeWise community, the ASP.NET Insiders group, and the INETA Speaker Bureau. In what spare time is left, he runs his own software and consultancy company Stonebroom Ltd.

Comments