Week 5 - MSXML to .NET - Martha's COIN 78b
CODES
Loading and Persisting Navigating Using XPath Transforming Using Datasets
Loading and Persisting XML Documents
- MSXML
load() - loads XML document into the parser loadXML() - loads XML from a string save() - persists an XML document into it's string form
- .NET - implements the DOM api in the XmlDocument object.
loadXML() - acts exactly as in MSXML. load() - exits in 3 different forms: load(string) - loads a string from a URL or a file, just as with MSXML; load(XmlReader) - takes an XmlReader object load(XmlTextReader) - takes an XmlTextReader object
reader = New XmlTextRead("c:\myxml.xml") Dim xmldoc As XmlDocument = new XmlDocument() xmldoc.Load (reader)save() - also exists in 3 different forms: save(string) - saves the file with the string/file name; save(XmlWriter) - abstract class; save(TextWriter) - abstract class; XmlTextWriter is a concrete class that implements it.
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
-
MSXML - methods that take as input a string that evaluates to an XPath expression
- selectNodes() - and returns a node-list,
- selectSingleNode() - and returns a node.
- .NET Framework - XPath functionalities have been moved from the XmlDocument class to:
- XPathDocument object - it provides a fast, read-only cache for XML document processing using XSLT and XPath. Use an XPathDocument constructor to create an instance of XPathDocument. It can be passed an XmlReader, TextReader, or even direct XML file names. For example:
Dim doc as XpathDocument = new XPathDocument("XmlDoc.xml") - XPathNavigator class - provides an alternative to the DOM api to navigate an XML document. A new instance can be created by calling XPathDocument.CreateNavigator method. For example:
Dim doc as XPathDocument = new XPathDocument("XmlDoc.xml") Dim nav as XPathNavigator = doc.CreateNavigator()Some of its methods include MoveToFirst, MoveToLast, MoveToId, MoveToRoot, MoveToNext, MoveToPrevious, MoveToParent, Select...Dim ni as XPathNodeIterator = nav.Select("/catalog/book/title") while (ni.MoveNext()) Console.WriteLine(ni.Current.Value) end while
Transforming XML Documents
The MSXML Way:
3 different XSL transformations are supported, extending the DOM api with a couple of methods:- transformNode()
- transformNodeToObject()
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)