perfectxml.com
 Basic Search  Advanced Search   
Topics Resources Free Library Software XML News About Us
home » Articles » XML & Web Services for Microsoft Developers – Part I Tuesday, 18 September 2007

XML & Web Services for Microsoft Developers – Part I

Author:
Article Date: November 29, 2002

Abstract

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. The 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 are a developer working on Microsoft platform, building Web or Windows applications, it is very 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.

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)

Complete details on the standards supported by MSXML can be found at https://perfectxml.com/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 by 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.w3.org</URL>
                <Title>World Wide Web Consortium</Title>
        </Site>
        <Site>
                <URL>msdn.microsoft.com</URL>
                <Title>MSDN</Title>
        </Site>
        <Site>
                <URL>perfectxml.com</URL>
                <Title>XML and Web services</Title>
        </Site>
        <Site>
                <URL>www.google.com</URL>
                <Title>Google Search Engine</Title>
        </Site>
        <Site>
                <URL>www.cnn.com</URL>
                <Title>CNN News</Title>
        </Site>
</Sites>
                        

The following ASP page code uses MSXML 4.0 DOM to load the sites.xml, process it and generate HTML output. MSXML 4.0 can be obtained from the MSDN Website.

Note that in the following ASP example, we are generating HTML output by loading and processing XML document using MSXML DOM; the other popular alternative is to transform XML into HTML by applying XSLT stylesheets. Click here for more details on that approach.


<%@ 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>My Favorite Websites</b><ul>"

    'For each Site node
        Dim aSiteNode
        Dim strURL

        For Each aSiteNode in siteNodes
                strURL = aSiteNode.selectSingleNode("URL").nodeTypedValue
                With Response
                        .Write "<li><a rel='nofollow' href='http://"
                        .Write strURL
                        .Write "'>" & aSiteNode.selectSingleNode("Title").nodeTypedValue
                        .Write "</a> (" & strURL & ")</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.

Save the above XML file (sites.xml) and the ASP page (ShowSites.asp) in the same IIS virtual folder, and browse to the ASP page, you should see the output similar to:

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 are 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 = "https://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 since we are loading a remote XML document over HTTP, it is required to 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 of using 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.w3.org</URL>
                <Title>World Wide Web Consortium</Title>
        </Site>
        <Site>
                <URL>msdn.microsoft.com</URL>
                <Title>MSDN</Title>
        </Site>
        <Site>
                <URL>perfectxml.com</URL>
                <Title>XML and Web services</Title>
        </Site>
        <Site>
                <URL>www.google.com</URL>
                <Title>Google Search Engine</Title>
        </Site>
        <Site>
                <URL>www.cnn.com</URL>
                <Title>CNN News</Title>
        </Site>
</Sites>
</XML>
</head>

<body>
  <div align="center">
  <h2>My Favorite Websites</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 a 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.

Save the above HTML text into a file and browse to this HTML page and you'll see the output similar to shown in the following screen:

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 still remains to be 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 retrieving relational data as XML.

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, OPENXML function was introduced that allows XML data to be used as a relational rowset, that can be SELECTed, INSERTed, or used for the relational UPDATE statement. There are three steps for using OPENXML. First is to load the XML document and get the document handle, 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 @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 Web site. 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 and https://perfectxml.com/SQLXML.asp 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 present asked five people to define Web services and she got six different answers! Even though the XML Web services is the hottest topic in the industry today, it sometimes gets tricky to define the Web services and explain the real value behind it.

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 now can easily call .NET code running on Windows 2000, over 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 gets back XML response package as the Web method results.

One of the primary pillars for Web services is SOAP, a W3C specification, which defines 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 response. There are many toolkits available that simplify this process of sending and receiving SOAP packages, and working with the resultant XML. Microsoft SOAP toolkit is one of these toolkits.

Microsoft SOAP Toolkit can be downloaded from MSDN Web site 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 using SOAP Toolkit to call a Web service. If you don' t have the SOAP Toolkit 3.0 installed, download and install it from the MSDN Web site, before running the following example.

In this example, we'll write the client for Weather – Temperature Web service available at 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, and SalCentral. Go ahead and try out some more examples by using SOAP Toolkit to call the Web services.

Summary

More and more developers are using XML in their applications. 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!

Top


  Contact Us |  | Site Guide | About PerfectXML | Advertise ©2004 perfectxml.com. All rights reserved. | Privacy