Library tutorials & articles
What's new in System.Xml 2.0
- Introduction, XML readers & writers
- XML Document Stores and Navigators
- Working with XML Schemas
Working with XML Schemas
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.
Related articles
Related discussion
-
Creating a Windows Service in VB.NET
by davidvanr (108 replies)
-
Nesting tables in a dataset
by Peterb74 (1 replies)
-
Nesting tables in a dataset
by Peterb74 (0 replies)
-
HL7 requirement - Urgent
by Akhil_Kothari (1 replies)
-
write to XML file vb.net
by acnetonline (2 replies)
Related podcasts
-
Episode 10: LINQ
K Scott leads us in a discussion of LINQ, including: What is it How introducing LINQ to .NET changed the framework LINQ Providers LINQ to XML LINQ to SQL - how it's different from EF, tips and tricks, when to use it Links: LINQpad 3rd Party LIN...
Events coming up
-
Nov
18
15 Minutes of Fame
Dresher, United States
This is a yearly tradition. We select 10 of the favorite speakers from monthly meetings, code camps, and hands on labs. Each one does a 15 minute talk on their favorite .NET technology. This is our 10th anniversary so we plan a gala event with special prizes and refreshments.
This thread is for discussions of What's new in System.Xml 2.0.