XML and Web Services for Microsoft Developers – Part 1

Tweet

Microsoft’s Internet Explorer version 4.0 was the first Web browser to support XML. Since then XML support has been introduced in various Microsoft products, such as Office XP and SQL Server 2000. Microsoft re-assured its commitment by including extensive support for XML in the .NET framework.

Keeping up with evolution of XML family of standards, Microsoft products support various other specifications, such as XPath, XSLT, XML Schemas, DOM, SAX, SOAP and Web services. This recent ZDNet story declared Microsoft as one of the winners in the Web services market and mentioned that "Microsoft is establishing strong position in the developing Web services market".

Considering these facts, if you’re a developer who works on Microsoft platform, building Web or Windows applications, it’s crucial that you understand the usage and applications of XML, and know about the level of XML support offered in various Microsoft products and SDKs.

The goal of this tutorial is to provide you with a complete picture of XML and Web services support made available in varied Microsoft products. More specifically we’ll discuss the following offerings:

  • MSXML or Microsoft XML Core Services
  • XML and Internet Explorer
  • SQL Server 2000 XML or SQLXML
  • SOAP Toolkit
  • .NET Framework
  • Web Services Toolkits (Office XP and Exchange Server)
  • BizTalk Server
  • Other tools and SDKs

In this first part, we’ll explore MSXML, Data Islands, SQLXML, and SOAP Toolkit. In the second part of this tutorial we’ll focus on .NET Framework and Web Services.

This tutorial makes the assumption that you are familiar with XML family of standards. If you’re not, read SitePoint’s Introduction to XML.

Let’s get started and talk about what is MSXML and how to use it in your Web and/or desktop applications.

Microsoft XML Core Services

As mentioned earlier, Internet Explorer 4.0 was the first browser to support XML. With IE 4.0, Microsoft provided a COM DLL named msxml.dll, a basic DOM (Document Object Model) implementation that allowed creating and parsing XML documents. Over years, Microsoft has greatly enhanced this COM-based free XML parsing API, and added support for various other XML standards.

MSXML, now known as Microsoft XML Core Services, is the paramount XML processing API available on Microsoft platform. In additions to DOM parsing, it also supports various other standards such as SAX, XPath, XSLT, XML Schemas and XML namespaces. MSXML SDK is shipped with various products such as Internet Explorer, Office XP, etc., and also can be downloaded from the MSDN Website at http://msdn.microsoft.com/xml. The current Internet Explorer 6.0 release ships MSXML version 3.0 SP2. And the latest MSXML version available is MSXML 4.0 SP1 that you can download from the MSDN site mentioned earlier.

MSXML can be used to:

  • Create, parse, navigate and update XML document using DOM or SAX API,
  • Transform XML documents (XSLT),
  • Extract data (XPath),
  • Validate XML documents using DTD, XDR , XML Schemas (XSD), and
  • HTTP data access (XMLHTTP and ServerXMLHTTP)

The Microsoft site also has complete details on the standards supported by MSXML.

In the following section, we’ll look at examples on how to use MSXML on the server-side in an ASP page, and on the client-side in a Visual Basic application. Let’s begin with an ASP page example.

Using MSXML on the Server Side

MSXML is a COM-based API and hence can be used from scripting languages such as VBScript, ECMAScript, and Perl. In this example, we’ll write VBScript code inside an ASP page, use MSXML DOM to load an XML document, and create HTML response to be sent to the client browser.

Let’s say you have the following XML document available as sites.xml under the same IIS virtual directory as the ASP page:

<?xml version='1.0' encoding='utf-8'?>  
<Sites>  
<Site>  
<URL>www.SitePoint.com</URL>  
<Title>SitePoint Hub</Title>  
</Site>  
<Site>  
<URL>www.WebmasterBase.com</URL>  
<Title>Build your Site</Title>  
</Site>  
<Site>  
<URL>www.eCommerceBase.com</URL>  
<Title>Make Money</Title>  
</Site>  
<Site>  
<URL>www.PromotionBase.com</URL>  
<Title>Increase Traffic</Title>  
</Site>  
<Site>  
<URL>www.ServicesBase.com</URL>  
<Title>Web Tools and Services</Title>  
</Site>  
<Site>  
<URL>www.SitePointForums.com</URL>  
<Title>Help Community</Title>  
</Site>  
</Sites>

The following ASP code uses MSXML 4.0 DOM to load the sites.xml, process it and generate HTML output. If you do not already have MSXML 4.0 installed, download and install it from the MSDN Website.

ShowSites.asp  
<%@ Language=VBScript %>  
<%  
Option Explicit  
Response.Expires = -1  
 
'Create MSXML 4.0 DOMDocument instance  
Dim objXMLDoc  
Set objXMLDoc = Server.CreateObject("MSXML2.DOMDocument.4.0")  
 
'Load sites.xml file present in the same directory as this ASP page  
objXMLDoc.async = False  
objXMLDoc.validateOnParse = False  
objXMLDoc.resolveExternals = False  
 
Dim bLoadResult  
bLoadResult = objXMLDoc.load(Server.MapPath("sites.xml"))  
 
'If Load successful  
If bLoadResult Then  
'Generate HTML Output  
 
'Select Site Nodes  
Dim siteNodes  
Set siteNodes = objXMLDoc.selectNodes("/Sites/Site")  
 
Response.Write "<b>SitePoint: <i>Empowering Web  
Developers since 1997.</i></b><ul>"  
 
'For each Site node  
Dim aSiteNode  
For Each aSiteNode in siteNodes  
With Response  
.Write "<li><a href='http://"  
.Write aSiteNode.selectSingleNode("URL").nodeTypedValue  
.Write "'>" & aSiteNode.selectSingleNode("Title").nodeTypedValue  
.Write "</a></li>"  
End With  
Next  
Response.Write "</ul>"  
 
Else  
'Load Unsuccessful, print error  
Response.Write "<font color='red'>" &  
objXMLDoc.parseError.reason & "</font>"  
End If  
 
%>

The above ASP page begins by creating an instance of class DOMDocument using the MSXML 4.0 version dependent ProgID MSXML2.DOMDocument.4.0. Next, we set certain properties to have the XML document loaded synchronously, and tell MSXML not to validate XML document and skip resolving any external references in the XML document.

As the document is being loaded from an external file, we use the load method, instead of loadXML which is used to load the XML document from a string. If the document load succeeds, we use the DOM selectNodes methods and pass it the XPath expression /Sites/Site that selects all the <Site> nodes from the source XML document. Finally, for each <Site> node, we assume that it contains <URL> and <Title> child nodes, and we select these node values to generate the HTML output.

If the document load fails, the error message string is generated using parseError interface.

Download the source code for this article, save the included XML (sites.xml) and ASP page (ShowSites.asp) under an IIS virtual directory, browse to ShowSites.asp and you should see the output similar to following screen:

950_screen1

Figure 1. HTML output generated by an ASP page using MSXML DOM

In this example, you learned about using MSXML DOM in an ASP page on the server side. Let’s now see an example of using MSXML in a Visual Basic application to be run on the client side.

Using MSXML on the Client Side

Let’s assume that you’re working on a Windows application that periodically needs to connect to a Web server, download some configuration details, and refresh the same on the client side. Let’s also assume that these configuration settings are saved as an XML document on the server. So the task in hand is to download this XML document from the server, and save it on the client side as a disk file. The following Visual Basic application does the exactly same job:

Start Visual Basic 6.0, create a new standard EXE project, and add reference (Project | References…) to Microsoft XML, v4.0 (msxml4.dll). Double click on the form and write the following code in the Form_Load() method:

Const strURL = "http://www.PerfectXML.com/books.xml"   
 
'Create MSXML DOMDocument instance  
Dim objXMLDoc As New MSXML2.DOMDocument40  
 
'Set Properties  
objXMLDoc.async = False  
objXMLDoc.validateOnParse = False  
objXMLDoc.resolveExternals = False  
 
'Since loading over HTTP from a remote server  
objXMLDoc.setProperty "ServerHTTPRequest", True  
 
If objXMLDoc.Load(strURL) Then  
   objXMLDoc.save "c:books.xml"  
   MsgBox "Remote XML document saved as c:books.xml."  
Else  
   MsgBox "Error: " & objXMLDoc.parseError.reason  
End If  
 
Unload Me

The above Visual Basic code, like ASP page example, uses MSXML DOM to load the XML document. The important point to note is that, as we’re loading a remote XML document over HTTP, we must set the ServerHTTPRequest property to True. If the document load succeeds, the save method is called to persist the loaded XML document on the client side as c:books.xml.

We just saw an example that used MSXML on the client side in a Visual Basic application. Let’s now focus on using XML inside the browser client.

XML and Internet Explorer

Beginning with Internet Explorer 5.0, Microsoft introduced the notion of XML Data Islands, which refers to the ability of including chunks of XML data inside the HTML Web page. These islands of XML data inside the Web pages then can be bound to HTML controls, such as tables, or processed using client-side JScript or VBScript. The Internet Explorer browser internally uses MSXML to offer the Data Island functionality.

Let’s now look at an example that makes use of Internet Explorer’s XML Data Island feature. For this example to work, you’ll need IE 5.0 or higher.

<html>    
<head>    
 <title>Data Island Example</title>    
 <style>    
   BODY, A, LI, TD  {    
     font-family: 'Verdana', 'Arial', sans-serif;    
     font-size: 9pt;    
     color : black;    
   }    
 </style>    
   
<XML ID="SITES">    
<Sites>    
 <Site>    
   <URL>www.SitePoint.com</URL>    
   <Title>SitePoint Hub</Title>    
 </Site>    
 <Site>    
   <URL>www.WebmasterBase.com</URL>    
   <Title>Build your Site</Title>    
 </Site>    
 <Site>    
   <URL>www.eCommerceBase.com</URL>    
   <Title>Make Money</Title>    
 </Site>    
 <Site>    
   <URL>www.PromotionBase.com</URL>    
   <Title>Increase Traffic</Title>    
 </Site>    
 <Site>    
   <URL>www.ServicesBase.com</URL>    
   <Title>Web Tools and Services</Title>    
 </Site>    
 <Site>    
   <URL>www.SitePointForums.com</URL>    
   <Title>Help Community</Title>    
 </Site>    
</Sites>    
</XML>    
</head>    
   
<body>    
 <div align="center">    
 <h2>SitePoint.com</h2>    
   
 <table width="100%" cellpadding="2"    
    cellspacing="1" border="0" bgcolor="#EEEEEE"    
    DATASRC="#SITES">    
   <thead>    
     <th>Title</th>    
     <th>URL</th>    
   </thead>    
   <tr>    
     <td bgcolor="#FFFFFF"><div DATAFLD="Title"></div></td>    
     <td bgcolor="#FFFFFF"><div DATAFLD="URL"></div></td>    
   </tr>    
 </table>    
 </div>    
</body>    
</html>

Inside an HTML Web page, we can include XML data using the <XML></XML> tag. The above HTML page contains the XML Data Island named SITES and then later binds this data to a table using the DATASRC and DATAFLD attributes.

Browse to the above HTML page and you’ll see the output similar to shown in the following screen:

950_screen2

Figure 2 XML Data Island bound to a table control inside an HTML page

This concludes our discussion on MSXML. Let’s now explore the XML features in SQL Server 2000.

XML and Databases

The primary problem with HTML is that it combines data with presentation. On the other hand, XML is all about data. XML has become the de facto format for portable data. One of the primary sources of data is still the relational databases. Keeping these facts in mind, Microsoft introduced support for XML in their relational DBMS, SQL Server 2000.

SQL Server 2000 allows relational data to be retrieved as XML, and XML to be directly imported into relational database.

The FOR XML clause was introduced to be used with the standard SELECT Transact SQL statement. This clause allows the retrieval of relational data as XML. Try out the following example:

Start SQL Server Query Analyzer tool, select the Northwind sample database and execute the following query:

SELECT * FROM [Customers] FOR XML AUTO

Instead of returning the standard relational rowset, you’ll notice that the data is returned as XML nodes.

To complement the FOR XML clause, the OPENXML function was introduced. This allows XML data to be used as a relational rowset, which can be SELECTed, INSERTed, or used for the relational UPDATE statement. There are three steps involved in using OPENXML. The first is to load the XML document and get the document handle. Then use OPENXML to convert the XML document into a relational rowset. And finally, free the XML document handle. Two system stored procedures, sp_xml_removedocument and sp_xml_preparedocument are used to work with the handles.

DECLARE @idoc int     
   
DECLARE @doc varchar (1000)    
   
SET @doc ='    
<ROOT>    
<ShipperRec Company="ABC Shippers" Ph="(503) 555-9191" />    
</ROOT>'    
   
--Create an internal representation of the XML document    
EXEC sp_xml_preparedocument @idoc OUTPUT, @doc    
   
-- Execute a SELECT statement that uses the OPENXML rowset provider    
SELECT *    
FROM OPENXML (@idoc, '/ROOT/ShipperRec',1)    
WITH (Company varchar(80),    
Ph varchar(48)    
)    
   
-- Clear the XML document from memory    
EXEC sp_xml_removedocument @idoc

Run the above commands in Query Analyzer, and you should see a record in the output window, with column names and data values from the XML document defined by the @doc variable above.

SQL Server 2000 also introduced ability to access relational data as XML over HTTP. A tool known as Configure SQL XML Support in IIS was added that allows configuring IIS virtual directories that map to relational database. This virtual directory then can be used to access the database over HTTP.

See SQL Server 2000 Books Online (Start | Programs | Microsoft SQL Server | Books Online) for more details on native XML support in SQL Server 2000.

To keep up with the fast-evolving XML standards, and to enhance the XML support in SQL Server 2000, Microsoft followed the Web release model (like with MSXML) and frequently releases the SQLXML extension via the MSDN Website. The current SQLXML 3.0 release supports ability to update the database over HTTP using XML (Updategrams), XML Bulk Import, exposing SQL Server stored procedure and functions as Web service methods, and more. Visit http://msdn.microsoft.com/sqlxml for more details on this.

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!

Free book: Jump Start HTML5 Basics

Grab a free copy of one our latest ebooks! Packed with hints and tips on HTML5's most powerful new features.

No Reader comments