Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

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

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.


advertisement

XML Transformations

XSLT is a template based language (with a XML compliant syntax) to trasform XML documents into another XML document, a HTML document or even to a non XML file (like a csv file). 
In msxml30 XSL transformations are supported, again, extending the DOM api with a couple of methods : transformnode and transformnodetoobject. You need to load the XML and the xsl files into two different dom instances and then call 



Output = Xmldoc.TransfromNode (Xsldoc)

There is actually an alternative  method to perform transformations in msxml30 that let's you cache compiled stylesheets if you need to apply the same transformation to different documents.
XSL transformations are applied in .NET via the XSLTransform class.
The Transform method of this class transforms the XML structure passed as an XMLNavigator object. The transformation result is provided as a XmlReader, a XmlWriter, a TextWriter or a Stream.
This sample transform an XML document into an HTML document that is directly transferred to the ASP response object.

Dim XMLDoc as new XmlDocument
XMLDoc.load("c:\myxml.xml")
Dim xslt as New
XslTransform 
Xslt.load("c:\myxsl.xml")
Dim nav as new DocumentNavigator(XMLDoc)
Xslt.Transform(nav,Nothing,response.output)
 

The 2nd parameter can be used to pass parameters into the XSL transformation. Note that this feature is not yet supported in beta1.

XML and Datasets

XML and Dataset (the evolution of Ado disconnected recordsets) have a lot in common. The Dataset holds internally its data in a XML format. The XMLDataDocument is a specialization of the XMLDocument class that provides a relational view of the XML structure; you can get a DataSet from it calling its DataSet property.

Dim doc as new XmlDataDocument
Dim myDs as DataSet
Doc.load("c:\myxml.xml")
MyDs = Doc.DataSet
Dim Books as DataTable
Books = DMyDs.Tables("books")


When you load XML data into a Dataset a schema is required to map an XML-tree into relational data. You can provide this schema calling the LoadDataSetMapping. If you don't do so, the XmlDataDocument object tries to infer the schema from the XML data structure. 
You can move on the opposite direction, feeding an XMLDataDocument with a DataSet, via one of the two constructors of the XMLDataDocument

Dim doc as new XMLDataDocument(myDataSet)

Conclusions

In this article I've provided a quick start to XML support in the .NET framework in order to show you how to perform in .NET the common XML tasks you currently do with the COM based Microsoft XML parser.
I suggest you to spend some of your "spare" time (a.k.a . after midnight) playing around with .NET XML support classes: due to the central role XML will play in the .NET world, XPATH, XSLT and schema will be as important as marshalling, interfaces and threading models are in the COM world.




Enrico Sabbadin is a software developer (MCP) that works, since 1995, on distributed and Internet based applications design on the Microsoft platform. He is an author for several Italian programming magazines and for the ASPToday web site, and occasionally speaks at Microsoft MSDN conferences in Italy. Enrico maintains at his own site a MTS FAQ (http://www.sabbasoft.com).
Comment and Contribute

 

 

 

 

 


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

 

 

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