BACK

Week 5 - MSXML to .NET - Martha's COIN 78b

CODES

Loading and Persisting XML Documents

Navigating an XML Document

XmlTextReader class provides an alternative api to navigate theXML document, compared to the DOM (which uses the XmlDocument object). Also, it provides an alternative to SAX, without requiring comples state management. However, a SAX parser can be built on top of XmlTextReader object. To use the XmlTextReader object, try this:
While reader.Read()
    If reader.NodeType = xml.XmlNodeType.Text then
        Console.Write(reader.Value)
        Console.WriteLine()
    End if
End While

Using XPath

To extract a node whose name is book and that has an attribute named author with a value of John:
//book[@author='John'] - an abbreviated syntax
/descendant::book[attribute::author='John'] - the full XPath syntax

Transforming XML Documents

The MSXML Way:

3 different XSL transformations are supported, extending the DOM api with a couple of methods: Load the XML and XSL files into two different DOM instances, and then call
output = xmldoc.TransformNode(xsldoc)
An alternative method lets you cache compiled stylesheets (to enable applying the same transformation to different documents).

The .NET Way

The XslTransform class can be used in the System.Xml.Xsl namespace. The Transform() method of this class transforms the XML structure passed as an XML Navigator object. The transformation result is provided as an XmlReader, an XmlWrite, a TextWriter, or a Stream.

Visual Basic:
 'Create a new XslTransform object.
 Dim xslt As New XslTransform()
 
 'Load the stylesheet.
 xslt.Load(CType("styles/myxsl.xsl", String))
 
 'Create a new XPathDocument and load the XML data to be fransformed.
 Dim mydata As New XPathDocument("inputdata.xml")
 
 'Create an XmlTextWriter which outputs to the console.
 Dim writer As New XmlTextWriter(Console.Out)
 
 'Transform the data and send the output to the console.
 xslt.Transform(mydata, Nothing, writer, Nothing)
 
C#
 //Create a new XslTransform object.
 XslTransform xslt = new XslTransform();
 
 //Load the stylesheet
 xslt.Load("styles/myxsl.xsl");
 
 //Create a new XPathDocument and load the XML data to be transformed.
 XPathDocument mydata = new XPathDocument("inputdata.xml");

// Create an XmlTextWriter which outputs to the console.
XmlWriter writer = new XmlTextWriter(Console.Out);

//Transform the data and send the output to the console
xslt.Transform(mydata,null,writer,null);
 
 

Using Datasets

The Dataset (the evolution of ACO disconnected recordsets) holds its data internally in XML format. The XmlDataDocument is a specialization of the XmlDocument class that provides a relational view of the XML structure. From it, you can get a DataSet by calling its DataSet property. A schema is required to map an XML-tree into relational data when you load XML data into a Dataset. Otherwise the XmlDataDocument object tries to infor the schema from the XML data structure.
Dim doc as new XmlDataDocument
Dim myDs as DataSet

'Load the schema.
doc.DataSet.ReadXmlSchema("schema.xsd")
doc.load("c:\myxml.xml")
myDs = doc.DataSet
Dim books as DataTable
books = myDs.Tables("books")
You can also go in the opposite direction, feeding an XmlDataDocument with a DataSet, via one of the two constructors of the XmlDataDocument.
Dim doc as new XMLDataDocument(myDataSet)