Thursday, June 16, 2005

XML .NET (the custom samples)

{

Here are the examples from the last piece of the course when we explored the .NET Framework's implementation of XML related parsers and techniques.

You can download the sample in this zip file which you can extract to its project form to look at the code. Most examples are "methods" implemented off of Module1.vb1.

Read and Write

The BasicReader() method shows an example of parsing an XML document with a XmlReader. Pay attention to the Select… Case expression to see how to filter nodes based on their types.

The ValidReader() method shows an example of a validating parser (XmlValidatingReader). Although this method does the legwork, our ValidationEventHandler, the method ValidError() is responsible for responding to any "events" related to the validation. I stressed the importance of understanding Delegates here, and did a quick example of what I meant which you can download in a different project, vbDel. If you don't mind looking at C#, you can look at some earlier entries I made about how delegates work. One of the best uses of delegates is to make an asynchronous call to any method you've written. Asynchronous delegates are how asynchronous web service calls are implemented.

The fake SAX Parser we wrote is also in the project, in its own file called SAXP.vb. It is not very complete but should give you an idea of how to get the SAX functionality out of the .NET Framework. If you'd ever want to – that's probably a remote possibility.

The WriteXML() method is a very basic example of how you can use the XmlTextWriter to create XML documents. This isn't too interesting, but if you start to combine the XmlTextReader and the XmlTextWriter, you can start to see the power and ease of the .NET APIs. In the second group, we wrote some code that would convert an attribute based document into an element based one. You will find this in a method called AttToElement().

Transforming

The SimpleTransform() method is an example of the simplest form of XSLT Transformations that can be done with the .NET Framework. Another method, called MemoryTransform(), is an example of how transformation can be done and processed in memory to return results in a string format. You will notice the use of the MemoryStream class in this example.

Serialization

We took two different looks at serialization. In the first we looked at how a built in class, the DataSet, has the ability to persist itself in XML format. Our method PersistDataSet() shows an example of this. In order to demonstrate the retrieval of a persisted DataSet, we wrote another method called ReadPersistedDataSet(). Along the way we talked about the Schema options of the WriteXml method of the DataSet. Although it was a barely noticeable blurb, make sure you investigate the XmlDataDocument class. It's a quick way to go from a DataSet to an XML Dom-like object and back again.

Once we'd seen that type of persistence, we looked at conventional serialization, or being able to take any old class and serialize it into some output stream. We wrote the PersistConventional() method to display this functionality with a class called Person, and then we depersisted in a method called DepersistConventional().

This was a good way to start working at understanding web services. I'll make another entry with the web services demos, but really quickly, the last few examples related to XML and .NET:

XML and SQL Server 2000

We looked at ways to retrieve XML data from the database, and I demonstrated the FOR XML clause of an sql statement. After we looked at a few examples, we wrote the GetXMLFromDB() method that would issue a FOR XML query and return results using the XmlReader. Which left us with one other thing on our "to do" list: inserting XML data into SQL Server directly.

In the project you will find a file called OPENXML.sql. This file contains a series of scripts to work through to understand how one can use the OPENXML function in SQL Server 2000 to build a "rowset" view on an XML document. Once this rowset functionality is understood, it's easy to see how it can be combined with Insert/Select statements to insert data. This is demonstrated in the InsertXMLContent() method.

So that was the tour de force. You can download it all in this zipped project. If you have questions, don't hesitate to email me.


}

No comments: