Article
XML and Web Services for Microsoft Developers - Part 1
XML and ADO
ActiveX Data Objects, or ADO, is the premier data-access API on the Microsoft platform. It is a COM-based automation-enabled wrapper over OLE-DB. Starting with ADO 2.5, Microsoft added functionality to save relational Recordset as XML. The Recordset save method can be used for this purpose. Similarly, XML data can be loaded into a relational recordset.
Let's look at a Visual Basic example that connects to SQL Server Northwind database and saves the Orders table as a XML file.
Start Visual Basic 6.0, create a new standard EXE project, add reference to Microsoft ActiveX Data Objects, and write the following code in the Form_Load() method:
Dim objRS As New ADODB.Recordset
objRS.Open "SELECT * FROM [ORDERS]", _
"PROVIDER=SQLOLEDB.1;SERVER=.;UID=sa;PWD=;DATABASE=Northwind;"
objRS.Save "c:\NWOrders.xml", adPersistXML
objRS.Close
MsgBox "Order data saved as c:\NWOrders.xml."
Set objRS = Nothing
Unload Me
The above ADO code connects to a relational database, opens the recordset and saves as the XML format into a file named c:\NWOrders.xml. You can modify the above example to connect to any other data source, such as Microsoft Access or Oracle, by just updating the connection string in the Recordset Open method.
XML Messaging using Microsoft SOAP Toolkit
In a recent Web Services conference, one presenter asked five people to define Web services and got six different answers!
XML Web services is the hottest topic in the industry today. The notion of XML Web services allows two applications to seamlessly communicate over Internet, without any platform and programming language worries. This means that, for instance, a Perl script running on a Linux machine can now easily call .NET code running on Windows 2000, over the Internet. This is made possible via two very successful standards, XML and HTTP.
Over HTTP, one application sends XML request package to the other application, mostly over Internet, and receives an XML response package as the result of the Web method.
One of the primary pillars for Web services is SOAP, a W3C specification, which defines the use of XML and Web protocols (such as HTTP) for XML messaging. Using Web services can be loosely termed as sending SOAP request and receiving back SOAP responses. There are many toolkits available that simplify this process of sending and receiving SOAP packages, as well as working with the resultant XML. The Microsoft SOAP toolkit is one of these.
The Microsoft SOAP Toolkit can be downloaded from MSDN Website at http://msdn.microsoft.com/soap. The current 3.0 release offers much more than a basic SOAP toolkit functionality. It allows COM objects to be used as XML Web services, sending and retrieving attachments, and more. Check out the MSDN Website for complete details on the toolkit offerings.
Let's look at an example of how the SOAP Toolkit can be used to call a Web service. If you don't have the SOAP Toolkit 3.0 installed, download and install it from the MSDN Website before you run the following example.
In this example, we'll write the client for Weather -- Temperature Web service available at the XMethods Website. Given the U.S Zip code, this Web service returns the current temperature information for that region.
Start Visual Basic 6.0, create a new standard EXE project, add reference to Microsoft SOAP Type Library v3.0 and write the following code in the Form_Load() method:
Dim objWebSvcClient As New MSSOAPLib30.SoapClient30
Dim dTemp As Double
objWebSvcClient.MSSoapInit _
"http://www.xmethods.net/sd/2001/TemperatureService.wsdl"
dTemp = objWebSvcClient.getTemp("60195")
MsgBox dTemp
Unload Me
You can see from the above Visual Basic code how easy it is to call Web service using the Microsoft SOAP Toolkit. You don't have to worry about packaging and unpacking SOAP request/response structures.
The above code creates an instance of SOAPClient30 class, initializes it by passing the Web service WSDL URL (WSDL can be thought of as synonymous to IDL for DCOM/CORBA), followed by calling the actual Web service method (getTemp), and getting the results.
There are hundreds of sample Web services listed on sites such as XMethods, BindingPoint, SalCentral, and more. Go ahead and try out some more examples using SOAP Toolkit to call the Web services.
Summary
More and more developers use XML in their applications, as both Web and desktop application developers are exploring the new possibilities made available by XML and Web services. Various Microsoft products, such as SQL Server 2000 and .NET natively support XML, and various toolkits, such as MSXML and SOAP Toolkit simplify working with XML.
In this two-part tutorial you'll learn about XML offerings available for Microsoft-platform developers. In this first part, you learned about MSXML, SQLXML, and SOAP toolkit. In the second part, we'll focus on .NET framework and Web services. Stay tuned!