Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

Migrating from Version 1.1 of the XML Classes

In versions after the .NET Framework version 1.1, there have been 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 sections of this document describe changes to the existing APIs that require you to upgrade existing code.

Microsoft .NET Framework version 3.5 includes new options for processing XML data. To learn more about these options see LINQ to XML.

With the introduction of the .NET Framework 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 as in the previous example, 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.

With the introduction of the .NET Framework 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.

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.

With the introduction of the .NET Framework 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 as in the previous example, 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.

The XslCompiledTransform class is the new XSLT processor and 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.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft