Browse DevX
Sign up for e-mail newsletters from DevX


From MSXML3.0 to .NET XML Classes: A Quick Guide

XML is a core technology in .NET; for example, XML is a cornerstone of Web Services and ADO.NET recordsets (now called DataSets) are internally stored as XML. You have great support for XML manipulation in .NET via its XML classes. This article provides a quick guide to migrate from msxml3.0 to XML classes since there have been some changes in how you can access functionalities such as XPATH and XSLT, how you can navigate an XML tree and how you can persist and load and XML documents.




Building the Right Environment to Support AI, Machine Learning and Deep Learning


As you have probably heard thousands of times, XML is a core technology in .NET. XML is a cornerstone of web services and ADO.NET recordsets (now called datasets) are internally stored as xml. You have great support for XML manipulation in .NET via its XML classes. In this article I want to provide a quick guide to migrate from msxml3.0 to XML classes since there have been some changes in how you can access functionalities such as XPATH and XSLT, how you can navigate an XML tree and how you can persist and load and   XML documents.

Loading and persisting XML documents

The w3c recommendations does not mandate a specific set of API to load and persist an XML document into the DOM. Each XML parser is free to implement its set of methods to accomplish this task. In the msxml parser you can load into the Dom  an XML document from a file using the load method while the loadxml method loads the DOM from a string. To persist an XML document into its "string form" to a file you can use the save method.
In .NET the DOM api is implemented in the XMLDocument object. XMLDocument exposes a loadxml that behaves exactly as in the msxml30 parser, the load method is slightly changed instead. This method exposes 3 overload . The first one takes a string (to load from a URL or a file), the second takes an XmlReader object and the third a TextReader object. Both these objects, as we will see later, provide a way to navigate the XML tree in a more efficient way then  the DOM. 
Note that both XmlReader and TextReader objects are abstract classes. XmlReader is implemented in a derived class called XmlTextReader. You can use the XmlTextReader object to load XML from a file in the following way:

reader = New XmlTextReader ("c:\myxml.xml") Dim xmldocument As XmlDocument = new XmlDocument() xmldocument.Load (reader)

XmlDocument exposes a save method with 3 overloads that are symmetric to the load method; they take, respectively, a string (the file name), a XmlWriter and a TextWriter. Again, XmlWriter and TextWriter are abstract classes. The XmlTextWriter class is a concrete one that implements XmlWriter.

Navigating a XML Document

The XmlTextReader class we have seen before provides an alternative  way to the standard Dom api (implemented in the XmlDocument object) to navigate the XML document with a fast, read-only, forward-only cursor. XmlTextReader is an alternative to SAX to provide a light-weighted  solution to XML navigation without requiring complex state management to the XML consumer program.
A SAX parser can be built on top of the XMLTextReader object. Microsoft has promised to provide a full working sample that will show how to do this.
Using the XMLTextReader object is pretty simple:

While reader.Read() If reader.NodeType = xml.XmlNodeType.Text then Console.Write(reader.Value) Console.WriteLine() End if End While

Note that the XmlReader is similar to the DataReader provided by ADO.NET

The Dom api provides some methods to support searching and filtering on the XML tree, but these methods are rather cumbersome and you need to write lot of tedious code to use them. As you probably know XPATH is a w3c standard that lets you extract a generic set of nodes from an XML-tree using a declarative syntax, similar to the one you use to navigate a file system. By instance, the syntax to extract a node-list containing all the nodes whose name is book and that have an attribute named author with a value of John is:


This is the abbreviated XPATH syntax. To express the same select criteria with the full XPATH syntax you would have written.


XPATH does not mandate a specific api to expose its functionalities to programmers. 
In msxml30 XPATH functionalities are exposed through a couple of methods that extend the DOMDocument specifications: selectnodes and selectsinglenodes. These two functions take as input a string that must evaluate to an XPATH expression and returns, respectively, a node-list and a node. 
In beta1 XPATH functionalities have been moved from the XMLDocument class to a new class called XmlNavigator.
The XmlNavigator class provides an alternative to the DOM api to navigate and edit an XML document.
Conceptually, the XMLNavigator extends the XmlReader class adding random access navigation (without building a node tree, but a single node on demand when moving to it). 
You apply XPATH selections using the select / selectsingle methods. These methods does not return a node-list, on the contrary, they just restrict the underlying data the XmlNavigator cursor can access (like a filter in an ado recordset). To acquire the selected nodes by an XPATH expression the MovetoNextSelected method must be called, as shown in the following sample.

Dim nav as new DocumentNavigator(doc) 'where doc is a XMLDocument
Nav.select("//book[@author='john']") Nav.MoveToSelected() ...etc& Nav.MoveToDocument 'reset the cursor context to the whole document

Furthermore, XmlmNavigator  provides methods to edit the XML-tree.
The XmlNavigator class is abstract. It is implemented in the DocumentNavigator class that accepts a XMLDocument in its constructor and in the DataDocumentNavigator that accepts a XMLDataDocument class (the XML counterpart of an ADO DataSet, more on it later)
This situation is likely to change in beta2. According to a post I've read in the Microsoft XML NET newsgroup, editing features will be removed from the XMLNavigator class, additionally, select and selectsingle methods will be exposed from the XmlDocument class.

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date