Visual C# 2005
How to Program, 2/e
In our XML Basics Tutorial, we introduced XML syntax. We also presented an overview of technologies used to parse, validate and format XML documents. In this tutorial, we provide XML markup for an article and for a business letter. We also demonstrate tools for validating XML markup. This tutorial is intended for people who have read our XML Basics Tutorial or who are familiar with basic XML syntax.
Download the examples for this tutorial here.
[Notes: This tutorial is an excerpt (Section 19.3) of Chapter 19, XML, from our textbook Visual C# 2005 How to Program, 2/e (pages 934-940). This tutorial may refer to other chapters or sections of the book that are not included here. Permission Information: Deitel, Harvey M. and Paul J., Visual C# How to Program, 2/E ©2006. Electronically reproduced by permission of Pearson Education, Inc., Upper Saddle River, New Jersey.]
19.3 Structuring Data (Continued)
This validator can validate XML documents against both DTDs and Schemas. To install it, run the downloaded executable file xml_validator.exe
and follow the steps to complete the installation. Once the installation is successful, open the validate_js.htm
file located in your XML Validator installation directory in IE to validate your XML documents. We installed the XML Validator at C:\XMLValidator
(Fig. 19.5). The output (Fig. 19.6) shows the results of validating the document using Microsoft's XML Validator. Visit www.w3.org/XML/Schema
for a list of additional validation tools.
Root element letter (lines 7-44 of Fig. 19.4) contains the child elements contact, contact, salutation, paragraph,paragraph,closing and signature. In addition to being placed between tags, data also can be placed in attributes-name-value pairs that appear within the angle brackets of start tags. Elements can have any number of attributes (separated by spaces) in their start tags. The first contact element (lines 8-17) has an attribute named type with attribute value"sender", which indicates that this contact element identifies the letter's sender. The second contact element (lines 19-28) has attribute type with value "receiver", which indicates that this contact element identifies
Validating an XML document with Microsoft's XML Validator.
Validation result using Microsoft's XML Validator.
the letter's recipient. Like element names, attribute names are case sensitive, can be any length, may contain letters, digits, underscores, hyphens and periods, and must begin with either a letter or an underscore character. A contact element stores various items of information about a contact, such as the contact's name (represented by element name), address (represented by elementsaddress1, address2, city, state and zip), phone number (represented by element phone) and gender (represented by attribute genderof element flag). Element salutation (line 30) marks up the letter's salutation. Lines 32-40 mark up the letter's body using two paragraph elements. Elements closing (line 42) and signature(line 43) mark up the closing sentence and the author's "signature," respectively.
Common Programming Error 19.6
Failure to enclose attribute values in double ("") or single ('') quotes is a syntax error.
Line 16 introduces the empty element flag. An empty element is one that does not contain any content. Instead, an empty element sometimes contains data in attributes. Empty element flag contains an attribute that indicates the gender of the contact (represented by the parent contact element). Document authors can close an empty element either by placing a slash immediately preceding the right angle bracket, as shown in line 16, or by explicitly writing an end tag, as in line 22
Note that the address2 element in line 22 is empty because there is no second part to this contact's address. However, we must include this element to conform to the structural rules specified in the XML document's DTD-letter.dtd (which we present in Section 19.5). This DTD specifies that each contact element must have an address2 child element (even if it is empty). In Section 19.5, you will learn how DTDs indicate that certain elements are required while others are optional.