XML Document Design

This section has two primary goals. First, you will learn about the XML Designer in Visual Studio .NET, including how to create a new XML document and populate that document with tags. Second, the section shows you how to create and read XML documents with both element-centric and attribute-centric designs.

XML Document Features

XML documents are merely text with tags to delimit strings. The fact that XML documents are comprised of text means that you can read and understand them without any special interpreter. The tags in XML documents make them easier to interpret than other kinds of text documents because the XML tags highlight particular kinds of values in a text document. If you have ever tried to read a text file that contains information for many columns with few or no delimiters and no meaningful delimiters, you will understand the value of using tags to highlight various kinds of text data. In the same way that tags make it easy for humans to read XML documents, they make it easy for computers to parse the data from an XML document into a database. In addition, industry forces such as the World Wide Web Consortium ( http://www.w3.org ) have promulgated standards to make processing data in XML documents even easier. These standards define and support XML. Furthermore, many of these standards are implemented in XML syntax, which makes understanding them easier once you learn the basics of XML.

When using XML documents to represent table data, it is normal to use tags to mark column values. Developers commonly refer to tags as elements. The element names can denote the column names in a table. The values that reside within elements can have types, just as the column values in a table can have data types. XML standards define a large set of built-in primitive types. The XML standards also define built-in derived types. Derived types are derived from primitive types or other derived types. For example, nonPositiveInteger and nonNegativeInteger both derive from the integer type. The integer type, in turn , derives from the decimal type, which is a primitive type that can designate arbitrary precision numbers . See the XML Data Types Reference section in the Visual Studio .NET documentation for more detail on XML types.

In addition to representing values with elements, you can denote them with attributes . Attributes specify properties for elements. An attribute is a matching name and value pair. The name designates the property name , and the value specifies the property value. In the context of representing database tables, attributes can represent column names and their corresponding values for an element designating a row in a table. Although standard practices are moving away from representing data values with attributes, it is still common to find attributes in XML documents. Attributes are particularly convenient for denoting properties of an element. Although elements can contain child elements, attributes do not have this capability. Instead, attributes relate to a single tag or element that they modify.

Creating an Element-Centric XML Document

You can start to create an XML document within a Visual Studio .NET project by choosing Project, Add New Item. Then, select XML File from the Templates collection in the dialog box. You can optionally assign a custom name. Otherwise, names in a project folder have default names, such as XMLFile1.xml and XMLFile2.xml. All the files for this appendix reside in a Microsoft Windows application project named XMLSamples.

If you accept the default name and click Open, Visual Studio .NET presents the shell for an XML document (XMLFile1.xml) in a designer containing XML and Data tabs in its lower left corner. Clicking the Data tab for an empty document presents a message stating that the document is not well formed because it is missing a root element. To correct this problem, switch back to the XML tab. Then, type any uniquely named tag within angle brackets (<>). The XML Designer responds by entering a matching closing tag (one with a leading slash, or /). XML documents require a unique element within which all other elements reside. You can name this element anything you want. I chose Root . Within your root element, you can enter other elements, such as those for rows and those for columns within rows.

The following XML document represents the values from the Shippers table in the Northwind database. Notice that the document starts with a declaration that it is an XML document. In fact, it is a version 1.0 XML document, and it uses the utf-8 coding convention for converting characters to byte streams. Visual Studio .NET inserted this declaration as a result of selecting the XML File template. Beginning (<?) and ending (?>) delimiters mark the start and end of the declaration.

Notice that the remainder of the document appears in a hierarchical arrangement. The unique Root element appears at the top level. The document contains three pairs of Shippers elements ”one for each row. Within each pair of Shippers elements are three more pairs of tags. These tags mark the values for individual columns. Although common, it is not mandatory to represent XML tags hierarchically. In any event, nearly all starting tags have an ending tag, unless a single tag denotes both a start and an end for an element (in which case, the element ends with />). When a single tag denotes an element, the tag can represent values through its attribute settings.

 <?xml version="1.0" encoding="utf-8" ?> <Root> <Shippers> <ShipperID>1</ShipperID> <CompanyName>Speedy Express</CompanyName> <Phone>(503) 555-9831</Phone> </Shippers> <Shippers> <ShipperID>2</ShipperID> <CompanyName>United Package</CompanyName> <Phone>(503) 555-3199</Phone> </Shippers> <Shippers> <ShipperID>3</ShipperID> <CompanyName>Federal Shipping</CompanyName> <Phone>(503) 555-9931</Phone> </Shippers> </Root> 

I will return to this document later in this appendix to show you how to do more with it than enter data. For now, close the document and choose to save the changes to the file. One of the main purposes of constructing this document is to introduce you to the XML Designer and to familiarize you with an element-centric layout for an XML document.

Creating an Attribute-Centric XML Document

Create the shell for a new XML document named XMLFile2. Arrange this XML document with the column values appearing as attributes instead of as elements. Although you will not commonly represent data values as attributes, you will find it useful to become comfortable with attribute-centric documents. The attribute assignments are typical in many kinds of XML documents ”particularly schema documents, which the next section introduces.

Start by specifying a unique root tag for the XML document. You can use the same Root element specified in the element-centric XML document. You can also designate each row element with a Shippers tag, as Figure A-1 shows. Because the column values for a row appear within an element as attribute values, you can close an element with a trailing slash (/). As with the element names, the attribute names are case sensitive. Because no validation exists on this XML document, there is no built-in check for the correct case when entering new rows. However, it is possible to specify that an XML document match a schema. The schema can catch variation data values that do not match the schema for an XML document. The next section demonstrates how to use a schema for this purpose. All column values for attributes appear within quotes. This is mandatory. The quotes requirement does not follow from a schema. Instead, quotes for attributes help to make an XML document well formed. All XML documents must be well formed.

click to expand
Figure A-1: XML view of an attribute-centric XML document with values based on the Shippers table
 


Programming Microsoft Visual Basic. NET for Microsoft Access Databases
Programming Microsoft Visual Basic .NET for Microsoft Access Databases (Pro Developer)
ISBN: 0735618194
EAN: 2147483647
Year: 2006
Pages: 111
Authors: Rick Dobson

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net