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.

XmlReader Creation

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.

Note

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.
Dim resolver As New XmlUrlResolver()
resolver.Credentials = CredentialCache.DefaultCredentials

' Create the XmlTextReader.
Dim reader As New XmlTextReader("https://serverName/data/books.xml")
reader.WhitespaceHandling = WhitespaceHandling.None
reader.XmlResolver = resolver
// Supply the credentials necessary to access the Web server.
XmlUrlResolver resolver = new XmlUrlResolver();
resolver.Credentials = CredentialCache.DefaultCredentials;

// Create the XmlTextReader.
XmlTextReader reader = new XmlTextReader("https://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.
Dim resolver As New XmlUrlResolver()
resolver.Credentials = CredentialCache.DefaultCredentials

' Create the XmlReader.
Dim settings As New XmlReaderSettings()
settings.IgnoreWhitespace = True
settings.XmlResolver = resolver
Dim reader As XmlReader = XmlReader.Create("https://serverName/data/books.xml", settings)
// 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("https://serverName/data/books.xml", settings);

For more information, see Creating XML Readers.

XML Validation

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.

Dim reader As New XmlValidatingReader(New XmlTextReader("books.xml"))
reader.ValidationType = ValidationType.Schema
reader.Schemas.Add("urn:books", "books.xsd")
AddHandler reader.ValidationEventHandler, AddressOf ValidationCallBack
While reader.Read()
End While
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 Shared Sub ValidationCallBack(ByVal sender As Object, ByVal e As ValidationEventArgs) 
  Console.WriteLine("Validation Error: {0}", e.Message)  
End Sub 'ValidationCallBack
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.

Dim settings As New XmlReaderSettings()
settings.ValidationType = ValidationType.Schema
settings.Schemas.Add("urn:books", "books.xsd")
AddHandler settings.ValidationEventHandler, AddressOf ValidationCallBack
Dim reader As XmlReader = XmlReader.Create("books.xml", settings)
While reader.Read()
End While
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 Shared Sub ValidationCallBack1(ByVal sender As Object, ByVal e As ValidationEventArgs) 
  Console.WriteLine("Validation Error: {0}", e.Message)  
End Sub 'ValidationCallBack1
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

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.

Note

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.

Dim writer As New XmlTextWriter("books.xml", Encoding.Unicode)
writer.Formatting = Formatting.Indented
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.

Dim settings As New XmlWriterSettings()
settings.Indent = True
settings.Encoding = Encoding.Unicode
Dim writer As XmlWriter = XmlWriter.Create("books.xml", settings)
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 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.
Dim xslt As New XslTransform()

' Create a resolver and set the credentials to use.
Dim resolver As New XmlUrlResolver()
resolver.Credentials = CredentialCache.DefaultCredentials

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

' Transform the file.
Dim doc As New XPathDocument(filename)
Dim writer As New XmlTextWriter("output.xml", Nothing)
xslt.Transform(doc, Nothing, writer, Nothing)
// 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("https://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.
Dim xslt As New XslCompiledTransform()

' Create a resolver and set the credentials to use.
Dim resolver As New XmlUrlResolver()
resolver.Credentials = CredentialCache.DefaultCredentials

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

' Transform the file.
Dim writer As XmlWriter = XmlWriter.Create("output.xml")
xslt.Transform("books.xml", writer)
// 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("https://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