- Sams Teach Yourself XML in 21 Days, Third Edition
- Table of Contents
- About the Author
- Acknowledgments
- We Want to Hear from You!
- Introduction
- Part I: At a Glance
- Day 1. Welcome to XML
- All About Markup Languages
- All About XML
- Looking at XML in a Browser
- Working with XML Data Yourself
- Structuring Your Data
- Creating Well-Formed XML Documents
- Creating Valid XML Documents
- How XML Is Used in the Real World
- Online XML Resources
- Summary
- Q&A
- Workshop
- Day 2. Creating XML Documents
- Choosing an XML Editor
- Using XML Browsers
- Using XML Validators
- Creating XML Documents Piece by Piece
- Creating Prologs
- Creating an XML Declaration
- Creating XML Comments
- Creating Processing Instructions
- Creating Tags and Elements
- Creating CDATA Sections
- Handling Entities
- Summary
- Q&A
- Workshop
- Day 3. Creating Well-Formed XML Documents
- What Makes an XML Document Well-Formed?
- Creating an Example XML Document
- Understanding the Well-Formedness Constraints
- Using XML Namespaces
- Understanding XML Infosets
- Understanding Canonical XML
- Summary
- Q&A
- Workshop
- Day 4. Creating Valid XML Documents: DTDs
- All About DTDs
- Validating a Document by Using a DTD
- Creating Element Content Models
- Commenting a DTD
- Supporting External DTDs
- Handling Namespaces in DTDs
- Summary
- Q&A
- Workshop
- Declaring Attributes in DTDs
- Day 5. Handling Attributes and Entities in DTDs
- Specifying Default Values
- Specifying Attribute Types
- Handling Entities
- Summary
- Q&A
- Workshop
- Day 6. Creating Valid XML Documents: XML Schemas
- Using XML Schema Tools
- Creating XML Schemas
- Dissecting an XML Schema
- The Built-in XML Schema Elements
- Creating Elements and Types
- Specifying a Number of Elements
- Specifying Element Default Values
- Creating Attributes
- Summary
- Q&A
- Workshop
- Day 7. Creating Types in XML Schemas
- Restricting Simple Types by Using XML Schema Facets
- Creating XML Schema Choices
- Using Anonymous Type Definitions
- Declaring Empty Elements
- Declaring Mixed-Content Elements
- Grouping Elements Together
- Grouping Attributes Together
- Declaring all Groups
- Handling Namespaces in Schemas
- Annotating an XML Schema
- Summary
- Q&A
- Workshop
- Part I. In Review
- Well-Formed Documents
- Valid Documents
- Part II: At a Glance
- Day 8. Formatting XML by Using Cascading Style Sheets
- Our Sample XML Document
- Introducing CSS
- Connecting CSS Style Sheets and XML Documents
- Creating Style Sheet Selectors
- Using Inline Styles
- Creating Style Rule Specifications in Style Sheets
- Summary
- Q&A
- Workshop
- Day 9. Formatting XML by Using XSLT
- Introducing XSLT
- Transforming XML by Using XSLT
- Writing XSLT Style Sheets
- Using <xsl:apply-templates>
- Using <xsl:value-of> and <xsl:for-each>
- Matching Nodes by Using the match Attribute
- Working with the select Attribute and XPath
- Using <xsl:copy>
- Using <xsl:if>
- Using <xsl:choose>
- Specifying the Output Document Type
- Summary
- Q&A
- Workshop
- Day 10. Working with XSL Formatting Objects
- Introducing XSL-FO
- Using XSL-FO
- Using XSL Formatting Objects and Properties
- Building an XSL-FO Document
- Handling Inline Formatting
- Formatting Lists
- Formatting Tables
- Summary
- Q&A
- Workshop
- Part II. In Review
- Using CSS
- Using XSLT
- Using XSL-FO
- Part III: At a Glance
- Day 11. Extending HTML with XHTML
- Why XHTML?
- Writing XHTML Documents
- Validating XHTML Documents
- The Basic XHTML Elements
- Organizing Text
- Formatting Text
- Selecting Fonts: <font>
- Comments: <!-->
- Summary
- Q&A
- Workshop
- Day 12. Putting XHTML to Work
- Creating Hyperlinks: <a>
- Linking to Other Documents: <link>
- Handling Images: <img>
- Creating Frame Documents: <frameset>
- Creating Frames: <frame>
- Creating Embedded Style Sheets: <style>
- Formatting Tables: <table>
- Creating Table Rows: <tr>
- Formatting Table Headers: <th>
- Formatting Table Data: <td>
- Extending XHTML
- Summary
- Q&A
- Workshop
- Day 13. Creating Graphics and Multimedia: SVG and SMIL
- Introducing SVG
- Creating an SVG Document
- Creating Rectangles
- Adobe's SVG Viewer
- Using CSS Styles
- Creating Circles
- Creating Ellipses
- Creating Lines
- Creating Polylines
- Creating Polygons
- Creating Text
- Creating Gradients
- Creating Paths
- Creating Text Paths
- Creating Groups and Transformations
- Creating Animation
- Creating Links
- Creating Scripts
- Embedding SVG in HTML
- Introducing SMIL
- Summary
- Q&A
- Workshop
- Day 14. Handling XLinks, XPointers, and XForms
- Introducing XLinks
- Beyond Simple XLinks
- Introducing XPointers
- Introducing XBase
- Introducing XForms
- Summary
- Workshop
- Part III. In Review
- Part IV: At a Glance
- Day 15. Using JavaScript and XML
- Introducing the W3C DOM
- Introducing the DOM Objects
- Working with the XML DOM in JavaScript
- Searching for Elements by Name
- Reading Attribute Values
- Getting All XML Data from a Document
- Validating XML Documents by Using DTDs
- Summary
- Q&A
- Workshop
- Day 16. Using Java and .NET: DOM
- Using Java to Read XML Data
- Finding Elements by Name
- Creating an XML Browser by Using Java
- Navigating Through XML Documents
- Writing XML by Using Java
- Summary
- Q&A
- Workshop
- Day 17. Using Java and .NET: SAX
- An Overview of SAX
- Using SAX
- Using SAX to Find Elements by Name
- Creating an XML Browser by Using Java and SAX
- Navigating Through XML Documents by Using SAX
- Writing XML by Using Java and SAX
- Summary
- Q&A
- Workshop
- Day 18. Working with SOAP and RDF
- Introducing SOAP
- A SOAP Example in .NET
- A SOAP Example in Java
- Introducing RDF
- Summary
- Q&A
- Workshop
- Part IV. In Review
- Part V: At a Glance
- Day 19. Handling XML Data Binding
- Introducing DSOs
- Binding HTML Elements to HTML Data
- Binding HTML Elements to XML Data
- Binding HTML Tables to XML Data
- Accessing Individual Data Fields
- Binding HTML Elements to XML Data by Using the XML DSO
- Binding HTML Tables to XML Data by Using the XML DSO
- Searching XML Data by Using a DSO and JavaScript
- Handling Hierarchical XML Data
- Summary
- Q&A
- Workshop
- Day 20. Working with XML and Databases
- XML, Databases, and ASP
- Storing Databases as XML
- Using XPath with a Database
- Introducing XQuery
- Summary
- Q&A
- Workshop
- Day 21. Handling XML in .NET
- Creating and Editing an XML Document in .NET
- From XML to Databases and Back
- Reading and Writing XML in .NET Code
- Using XML Controls to Display Formatted XML
- Creating XML Web Services
- Summary
- Q&A
- Workshop
- Part V. In Review
- Appendix A. Quiz Answers
- Quiz Answers for Day 1
- Quiz Answers for Day 2
- Quiz Answers for Day 3
- Quiz Answers for Day 4
- Quiz Answers for Day 5
- Quiz Answers for Day 6
- Quiz Answers for Day 7
- Quiz Answers for Day 8
- Quiz Answers for Day 9
- Quiz Answers for Day 10
- Quiz Answers for Day 11
- Quiz Answers for Day 12
- Quiz Answers for Day 13
- Quiz Answers for Day 14
- Quiz Answers for Day 15
- Quiz Answers for Day 16
- Quiz Answers for Day 17
- Quiz Answers for Day 18
- Quiz Answers for Day 19
- Quiz Answers for Day 20
- Quiz Answers for Day 21
Creating XML Web Services
The last .NET XML topic we'll take a look at today are XML Web services. These services let you provide accessible services on the Web, and they can be used by other applications.
For example, say you were in the field and wanted to retrieve data from a database back at the central office. A Web service can help you do that, as long as the Web server the service is on has access to that database. All you have to do is connect to the Web service from a laptop, and you can get all the data you want. Even a Windows application can call the methods you put into a Web service, allowing you to integrate Web access into Windows applications easily. Web services are often used to implement multitiered, distributed data applications, and in the following sections we'll create a Web service much like that. All the data in this example will be passed back and forth by using XML.
Creating a Web Service
Our sample XML Web service is called ch21_06, and we'll use it to gain access to the authors table in the SQL pubs sample database. In particular, we'll implement two methods in this Web service—GetAuthors, to return a dataset that holds the authors table, and UpdateAuthors, to update that table in the pubs database when needed. Our XML Web service will be on the Web, so if a Windows application needs that data, it can just use our Windows service.
You can create a new Web service project by selecting File, New, Project, selecting the ASP.NET Web Service icon, making the project a Visual Basic .NET project, and giving this project the name ch21_06. When you click the OK button in the New Project dialog box, the new Web service project shown in Figure 21.20 is created.
Figure 21.20 Creating a new Web service project.
The Visual Basic .NET code file for your new Web service is Service1.asmx.vb, and the name of your new Web service is Service1. When you open Service1.asmx.vb in Visual Studio .NET, you see that this new service is derived from the WebService class:
Imports System.Web.Services <System.Web.Services.WebService(Namespace := "http://tempuri.org/ch21_06/Service1")> _ Public Class Service1 Inherits System.Web.Services.WebService . . .
In this Web service, we'll set up our connection to the authors table in the pubs sample SQL Server database. To do that, drag an OleDbDataAdapter data adapter to the Web service designer and use the Data Adapter Configuration Wizard to connect the data adapter to the authors table, as you did earlier today for the employee table. Then select Data, Generate Dataset to create a new dataset class, DataSet1. This is the dataset class you'll use to access the authors table in the Web service.
To make the methods in a Web service accessible outside the service, you use the <WebMethod()> attribute when declaring those methods. For example, here is how you do this for the GetAuthors method:
<WebMethod(Description:="Sends the authors table to the client")> _ Public Function GetAuthors() As DataSet1 . . . End Function
In the GetAuthors method, you want to return a dataset filled with the authors table, so add this code in Service1.asmx.vb:
<WebMethod(Description:="Sends the authors table to the client")> _ Public Function GetAuthors() As DataSet1 Dim AuthorsDataTable As New DataSet1 OleDbDataAdapter1.Fill(AuthorsDataTable) Return AuthorsDataTable End Function
As you're going to see, the GetAuthors method will be available to code in other applications after you've added a Web reference to the Web service in those applications.
You can also implement the UpdateAuthors method, which will update the authors table with changes the user has made. We can pass this method to a dataset that holds changes to the authors table and update the authors table by using the data adapter's Update method, like this:
<WebMethod(Description:="Updates the authors table from the client")> _ Public Function UpdateAuthors(ByVal _ ChangedAuthorsRecords As DataSet1) As DataSet1 If (ChangedAuthorsRecords Is Nothing) Then Return Nothing Else OleDbDataAdapter1.Update(ChangedAuthorsRecords) Return ChangedAuthorsRecords End If End Function
We have now implemented the GetAuthors and UpdateAuthors methods. To make this Web service available to applications, you should build the service now by selecting Build, Build ch21_06. Listing 21.5 shows the code we've developed so far for this example.
Example 21.5. An XML Web Service (from Service1.asmx.vb, ch21_06 Project)
Imports System.Web.Services . . . <WebMethod(Description:="Sends the authors table to the client")> _ Public Function GetAuthors() As DataSet1 Dim AuthorsDataTable As New DataSet1 OleDbDataAdapter1.Fill(AuthorsDataTable) Return AuthorsDataTable End Function <WebMethod(Description:="Updates the authors table from the client")> _ Public Function UpdateAuthors(ByVal _ ChangedAuthorsRecords As DataSet1) As DataSet1 If (ChangedAuthorsRecords Is Nothing) Then Return Nothing Else OleDbDataAdapter1.Update(ChangedAuthorsRecords) Return ChangedAuthorsRecords End If End Function End Class
Using a Web Service
Now we'll put our new Web service to work and call it from a Windows application. To make that work, add a new Windows project that will connect to the Web service by selecting File, Add Project, New Project.
In the Add New Project dialog box, select the Windows Application icon, name this new Windows application ch21_07, and click OK. The Windows application opens, as shown in Figure 21.21. Because the Web service has no visual interface, you need to make this Windows application the startup project by selecting Project, Set as StartUp Project. Now when you select Debug, Start, the Windows application will appear.
Figure 21.21 The ch21_07 Windows application.
We want to call the GetAuthors and UpdateAuthors methods in our Windows application. To do that, we need to add a Web reference to our Web service. To do this, right-click the ch21_07 entry in the Solution Explorer at the right in Visual Studio, and then select the Add Web Reference menu item. This opens the Add Web Reference dialog box, which lists the available Web service locations.
To add a reference to a Web service, you can enter the URL for the service's .vsdisco (Visual Studio discovery) file in the Address box in the Add Web Reference dialog box. Or you can browse to the service you want to use by clicking the link in the Add Web Reference dialog box for a server and then click the name of the service you want to use (in this case, Service1).
Either technique opens your Web service's entry in the Add Web Reference dialog box, as shown in Figure 21.22. To add a reference to this Web service to your Windows application, click the Add Reference button.
Figure 21.22 The Add Web Reference dialog box.
The Web reference is added to our Windows application, and this reference will give us access to the GetAuthors and UpdateAuthors methods. To hold the data returned from the Web service, add a data grid to the Windows application and place above the data grid two buttons with the captions Get the Data and Set the Data.
Next, drag a dataset object—not a data adapter this time—from the Data tab of the toolbox to the main form in the Windows application. When you do, the Add Dataset dialog box opens, as shown in Figure 21.23. In that dialog box, select the dataset object in your Web service, DataSet1, from the drop-down list (the fully qualified name of DataSet1 is ch21_07.localhost.DataSet1, as shown in Figure 21.23).
Figure 21.23 Using the Add Dataset dialog box.
To create the new dataset we'll use in our Windows application, DataSet11, click the OK button in the Add Dataset dialog box. We'll bind DataSet11 to the data grid in the Windows application, so set the data grid's DataSource property to DataSet11 and its DataMember property to authors in the properties window.
You're almost done. When you click the Get the Data button, the dataset, DataSet11, should fill with data sent to us from the Web service. To do that, you create an object, service, of your Web service class:
Private Sub Button1_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button1.Click Dim service As New ch21_07.localhost.Service1 . . . End Sub
The service object will let us use the methods we've built into our Web service. We can fill the Windows application's DataSet11 object with the dataset returned from the Web service's GetAuthors method by using the Merge method, like this:
Private Sub Button1_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button1.Click Dim service As New ch21_07.localhost.Service1 DataSet11.Merge(service.GetAuthors()) End Sub
That's all it takes—you create an object corresponding to the Web service, and then you can use that object's methods and access the Web service in your code.
We can update the authors table as needed. When the user edits the data in the data grid, those changes are also made to the DataSet11 object. To extract the changed records from that dataset, we can use the dataset's GetChanges method, like this:
Private Sub Button2_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button2.Click If DataSet11.HasChanges() Then Dim dsUpdates As New _ Ch21_07.localhost.DataSet1 dsUpdates.Merge(DataSet11.GetChanges()) . . . End If End Sub
Now we can use the Web service's UpdateAuthors method to update the authors table in the pubs database. That method returns the changed records, and we can merge them into the Windows application's dataset so those records will no longer be marked as changed:
Private Sub Button2_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button2.Click If DataSet11.HasChanges() Then Dim service As New Ch21_07.localhost.Service1 Dim dsUpdates As New _ Ch21_07.localhost.DataSet1 dsUpdates.Merge(DataSet11.GetChanges()) DataSet11.Merge(service.UpdateAuthors(dsUpdates) ) End If End Sub
The complete code for this example is shown in Listing 21.6.
Example 21.6. Using an XML Web Service (from Form1.vb, ch21_07 Project)
Public Class Form1 Inherits System.Windows.Forms.Form . . . Private Sub Button1_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button1.Click Dim service As New ch21_07.localhost.Service1 DataSet11.Merge(service.GetAuthors()) End Sub Private Sub Button2_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button2.Click Dim service As New ch21_07.localhost.Service1 Dim dsUpdates As New _ ch21_07.localhost.DataSet1 dsUpdates.Merge(DataSet11.GetChanges()) DataSet11.Merge(service.UpdateAuthors(dsUpdates)) End Sub End Class
When you run this example and click the Get the Data button, the data from the authors table should appear in the data grid, as shown in Figure 21.24. If you edit the data in the data grid and click the Set the Data button, your edits will be sent back to the authors table in the pubs database.
Figure 21.24 Connecting to and using a Web service.
As far as the users are concerned, the connection to the Web is maintained entirely behind-the-scenes; all the users have to do is click buttons. That's how XML Web services work. Behind-the-scenes in this example, all the data was sent back and forth by using XML. All in all, this is a very impressive showing for .NET programming.