Export (0) Print
Expand All
2 out of 2 rated this helpful - Rate this topic

Migrating from Version 1.1 of the XML Classes 

The Microsoft .NET Framework version 2.0 includes many updates to the XML classes. In order to take full advantage of the new features, you may need to move your existing code to the new APIs. The following sections describe changes to the existing APIs that may require you to upgrade existing code.

XmlReader Creation

In version 2.0, XmlReader objects are created using the static Create method on the XmlReader class. An XmlReaderSettings object specifies the features that you want the created XmlReader to support.

NoteNote

Although the .NET Framework includes concrete implementations of the XmlReader class, such as the XmlTextReader, XmlNodeReader, and the XmlValidatingReader classes, we recommend that you create XmlReader instances using the Create method. This allows you to take advantage of all the new features that have been added to the XmlReader class.

Version 1.1

The following code constructs an XmlTextReader object that ignores white space and uses an XmlUrlResolver object to resolve the filename URI.

// Supply the credentials necessary to access the Web server.
XmlUrlResolver resolver = new XmlUrlResolver();
resolver.Credentials = CredentialCache.DefaultCredentials;

// Create the XmlTextReader.
XmlTextReader reader = new XmlTextReader("http://serverName/data/books.xml");
reader.WhitespaceHandling = WhitespaceHandling.None;
reader.XmlResolver = resolver;

Version 2.0

The following code creates an XmlReader object with the same configuration, using the Create method.

// Supply the credentials necessary to access the Web server.
XmlUrlResolver resolver = new XmlUrlResolver();
resolver.Credentials = CredentialCache.DefaultCredentials;

// Create the XmlReader.
XmlReaderSettings settings = new XmlReaderSettings();
settings.IgnoreWhitespace = true;
settings.XmlResolver = resolver;
XmlReader reader = XmlReader.Create("http://serverName/data/books.xml", settings);

For more information, see Creating XML Readers.

XML Validation

In version 2.0, the XmlValidatingReader and XmlSchemaCollection classes have been marked obsolete. Instead, you should use the XmlReaderSettings class to create a validating XmlReader object. The XmlSchemaCollection class is replaced by the XmlSchemaSet class.

NoteNote

There are some behavioral changes between validation using the XmlReaderSettings and XmlSchemaSet classes and validation using the XmlValidatingReader class.

The XmlReaderSettings and XmlSchemaSet classes do not support XML-Data Reduced (XDR) schema validation.

There are differences in how elements, with xsi:nil='true' specified, are validated. Elements with white space content are considered valid by the XmlValidatingReader class. Elements with white space content are not considered valid by validating readers created using the XmlReaderSettings class.

Version 1.1

The following code validates data using the XmlValidatingReader and XmlSchemaCollection classes.

XmlValidatingReader reader = new XmlValidatingReader(new XmlTextReader("books.xml"));
reader.ValidationType = ValidationType.Schema;
reader.Schemas.Add("urn:books", "books.xsd");
reader.ValidationEventHandler += new ValidationEventHandler (ValidationCallBack);
while (reader.Read());

private static void ValidationCallBack(object sender, ValidationEventArgs e) {
    Console.WriteLine("Validation Error: {0}", e.Message);
}

Version 2.0

The following code validates data using the XmlReader and XmlSchemaSet classes.

XmlReaderSettings settings = new XmlReaderSettings();
settings. ValidationType = ValidationType.Schema;
settings.Schemas.Add("urn:books", "books.xsd");
settings.ValidationEventHandler += new ValidationEventHandler (ValidationCallBack);
XmlReader reader = XmlReader.Create("books.xml",settings);
while (reader.Read());

private static void ValidationCallBack1(object sender, ValidationEventArgs e) {
    Console.WriteLine("Validation Error: {0}", e.Message);
}

For more information, see Validating XML Data with XmlReader and XmlSchemaSet for Schema Compilation.

XmlWriter Creation

In version 2.0, XmlWriter objects are created using the static Create method on the XmlWriter class. An XmlWriterSettings object specifies the features that you want the created XmlWriter to support.

NoteNote

Although the .NET Framework includes the XmlTextWriter class, which is an implementation of the XmlWriter class, we recommend that you create XmlWriter objects with the Create method. This allows you to take advantage of all the new features added to the XmlWriter class.

Version 1.1

The following code creates an XmlTextWriter object with a specific encoding.

XmlTextWriter writer = new XmlTextWriter("books.xml", Encoding.Unicode);
writer.Formatting = Formatting.Indented;

Version 2.0

The following code creates an XmlWriter object with the same configuration, using the Create method.

XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.Encoding = Encoding.Unicode;
XmlWriter writer = XmlWriter.Create("books.xml", settings);

For more information, see Creating XML Writers.

XSLT Processing

The XslCompiledTransform class is the new XSLT processor. It replaces the XslTransform class. The XsltSettings enumeration is used to enable optional XSLT settings such as support for embedded scripts or the XSLT document() function.

Version 1.1

The following code performs an XSLT transformation using the XslTransform class.

// Create the XslTransform.
XslTransform xslt = new XslTransform();

// Create a resolver and set the credentials to use.
XmlUrlResolver resolver = new XmlUrlResolver();
resolver.Credentials = CredentialCache.DefaultCredentials;

// Load the style sheet.
xslt.Load("http://serverName/data/xsl/sort.xsl", resolver);

// Transform the file.
XPathDocument doc = new XPathDocument(filename);
XmlTextWriter writer = new XmlTextWriter("output.xml", null);
xslt.Transform(doc, null, writer, null);

Version 2.0

The following code performs an XSLT transformation using the XslCompiledTransform class.

// Create the XslCompiledTransform object.
XslCompiledTransform xslt = new XslCompiledTransform();

// Create a resolver and set the credentials to use.
XmlUrlResolver resolver = new XmlUrlResolver();
resolver.Credentials = CredentialCache.DefaultCredentials;

// Load the style sheet.
xslt.Load("http://serverName/data/xsl/sort.xsl", XsltSettings.Default, resolver);

// Transform the file.
XmlWriter writer = XmlWriter.Create("output.xml");
xslt.Transform("books.xml", writer);

For more information, see Migrating From the XslTransform Class.

See Also

Other Resources

XML Documents and Data

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.