Browse DevX
Sign up for e-mail newsletters from DevX


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.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

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:

<form runat="server"> Starting from week: <asp:TextBox id="txtWeek" Text="4" Columns="1" runat="server" /> year: <asp:TextBox id="txtYear" Text="2004" Columns="3" runat="server" /> <asp:Button id="btnGo" Text="Go" OnClick="ShowData" runat="server" /><p /> </form> <asp:Xml id="xmlResult" runat="server" />

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 DataSet—there'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() oXmlDoc.Load(sURL) xmlResult.Document = oXmlDoc xmlResult.TransformSource = "style-dataset.xsl" End Sub

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.
Thanks for your registration, follow us on our social networks to keep up-to-date