Synchronize Your Databases with .NET Web Services (Part I) : Page 8
The ever-increasing use of XML is an exciting development in Web site design and construction, and provides new ways for site authors to expose information to visitors. In this two-part article, you'll see how to create a data-driven Web service and explore three different ways to consume it. Then, in part II, you'll see how to use such services to synchronize the content in distributed databases automatically.
by Alex Homer
Mar 10, 2004
Page 8 of 8
Accessing a Web Service as an XML Document
The previous two examples have demonstrated how you can access a Web service using SOAP. But this example (aspnet-xml-client.aspx in the aspnet-client folder) uses a technique that doesn't rely on SOAP to access the Web service, or on using a .NET DataSet object to handle the results. It uses an ASP.NET page to access the Web service, but does so using the HTTP GET protocol rather than SOAP. Then it handles the results as an XML document, and not as a DataSet. The page contains basically the same controls as the preceding ASP.NET example, except that now there is an ASP.NET Xml server control instead of a DataGrid control:
When the user clicks the submit button, the code builds a string containing the URL of the Web service. As shown earlier, the URL must contain the method name and any parameter values, in the following format:
So the example specifies the trafficparameters.asmx Web service file, the TrafficSummaryFromWeekYear method, and adds as the query string the values extracted from the "week" and "year" text boxes. Then it creates a new XmlDocument instance and loads it with the returned XML . This time, the XML consists of the schema and data that represents the DataSetthere's no SOAP envelope or other containing elements because you're making an HTTP GET request this time, not a SOAP request:
<%@Import Namespace="System.Xml" %>
Sub ShowData(sender As Object, args As EventArgs)
Dim sURL As String =
"http://localhost/SynchWebService/webservices/" & _
"trafficparameters.asmx/TrafficSummaryFromWeekYear?" & _
"Week=" & txtWeek.Text & "&Year=" & txtYear.Text
Dim oXmlDoc As New XmlDocument()
xmlResult.Document = oXmlDoc
xmlResult.TransformSource = "style-dataset.xsl"
After loading the XML (it would, of course, be a good idea to use a Try...Catch construct here to trap any errors), you can take advantage of the clever ASP.NET Xml server control to display it. The Xml control takes the XML document, plus the XSLT stylesheet you specify as the TransformSource property, and performs the transformation. The result is inserted into the page returned to the client. Figure 7 shows the result. This example uses the same style sheet as in the preceding IE5 Web service behavior example, so it generates the same output.
Alex Homer is a director of Stonebroom, Ltd., a software development, consulting, and training organization. He was formerly lead technical author and reviewer for Wrox, specializing in Microsoft Web and database technologies. You can reach him through his Web site.