This chapter briefly examines the web service protocols. Although the .NET environment shields you from the need to understand and work directly with the protocols, your understanding of the protocols’ basic processing will help you master the advanced techniques this book’s later chapters present. This chapter’s goal is that you begin to understand the behind-the-scenes operations that occur when your programs call a web service. Do not be too concerned at this point about each protocol’s specific processing. As you have already found, within Visual Studio .NET, you can create and use web services with no knowledge of the underlying protocols.
Programmers often refer to web services as XML web services because behind the scenes, the information your programs send to and receive from a web service is packaged using XML, the extensible markup language. Further, the WSDL files you have used to add web references for services within your programs actually consist of XML-based content that describes the service and its methods, such as the number and types of parameters each method requires and the type of value each method returns.
XML is a very powerful markup language and XML-based solutions can become quite complex. Fortunately, to create and use web services within the .NET environment, you do not have to interact with the XML directly. This section briefly examines XML. By the time you finish this section and examine one or two WSDL files that define web services with which you are familiar, you will be able to understand each file’s key elements at a glance.
XML, like HTML, is a markup language. Whereas web developers use HTML to implement web pages, programmers use XML to describe data. Using XML, a programmer, for example, can create a document that describes a database’s fields and data structure. Later, a database program can import the XML document and use the information it contains to build the corresponding fields and records within a database.
XML exists to describe data. A program might use XML to describe a video clip by providing information about the clip’s length, number of frames per second, compression technique, and so on. Likewise, a different program might use XML to describe a graphics image by providing specifics about the image’s resolution, number of colors, and file size.
Behind the scenes, web services make extensive use of XML. Fortunately, the .NET environment hides many of these behind-the-scenes operations from programmers. That said, as you examine web services in detail, you will encounter XML entries. To better understand the entries, turn to the book Mastering XSLT by Chuck White (Sybex 2002), which covers XML, XML stylesheets, and XML transformations in detail.
XML, like HTML, is based on tags. Unlike the HTML tags such as <b>, <i>, or <img> that are the same within web pages, XML tags will normally differ from one document to the next, because the data that each document describes is different. Like HTML, XML uses starting and ending tags that appear between left and right angle brackets. A closing tag precedes the tag name with a forward slash (/) character as shown here:
The XML documents you create should begin with the following statement that specifies the XML version to which the document conforms:
Listing 4.1 is a series of XML statements describing a Book object that contains a title, price, publisher, and author.
Listing 4.1 Book.xml
<?xml version="1.0"?> <Book> <Title>Visual C# .NET Programming</Title> <Price>39.99</Price> <Publisher>Sybex</Publisher> <Author>Harold Davis</Author> </Book>
You can create an XML document using a variety of text editors, such as the Windows Notepad accessory. Using Notepad, create the file EmailMessage.xml that contains the entries in Listing 4.2.
Listing 4.2 EmailMessage.xml
<?xml version="1.0"?> <Message> <To>email@example.com</To> <From>firstname.lastname@example.org</From> <Subject>Chapter 4</Subject> <Content>Hi, please find the attached Word document that contain Chapter 4.</Content> <Attachment>Chapter4.doc</Attachment> </Message>
As you can see, the document uses the <Message> tag to denote the start of the object. Within the message object, additional tags describe each of the message’s key fields. Note that each message component is enclosed within starting and closing tags. Finally, the close </Message> tag marks the end of the data object.
Using a web browser, such as Internet Explorer, you can display an XML document’s contents as shown in Figure 4.1.
Figure 4.1: Displaying an XML document within a browser
When you display an XML document within Internet Explorer, the browser will display the XML tags and the corresponding data. That’s because, unlike HTML tags that the browser uses to format data, the browser does ignore the XML tags, because the tags correspond to a data object.
To display XML data, Internet Explorer uses built-in software called an XML parser. When you create XML documents, you can take advantage of the parser to help you detect errors within your document, such as a misspelled or missing tag. For example, the XML file in Listing 4.3, Meal.xml, uses XML tags to describe a dinner meal.
Listing 4.3 Meal.xml
<?xml version="1.0"?> <Meal> <Salad>Caeser</Salad> <Soup>None</Soup> <Entre>Fettucine Alfredo</Entre> <Beverage>Ice Tea</Beverage> <Dessert>Apple Pie</Desert> </Meal>
If you examine the XML tags closely, you may notice that the closing </Dessert> tag is misspelled, using only one s. If you display the file’s contents within Internet Explorer, the browser’s built-in parser will detect and highlight the error, as shown in Figure 4.2.
Figure 4.2: Using the Internet Explorer’s XML parser to detect errors within an XML document
With HTML files, various tags support attributes to which you can assign values using an equal sign, as shown here:
<img src="/books/1/357/1/html/2/images/logo.gif" width=276 height=110 alt="Our Logo"> <input maxLength=256 size=55 name=Email value="">
In a similar way, XML tags can support attributes. The XML file in Listing 4.4, Video.xml, uses XML tags and attributes to describe a video clip.
Listing 4.4 Video.xml
<?xml version="1.0"?> <Video format="avi"> <Filename>Baseball.avi</Filename> <Resolution>320x240</Resolution> <Colors>256</Colors> <Compression version="1.0">vcodec</Compression> </Video>