XML Syntax The contents of an XML document are constructed using a very strict syntax that must conform to the following rules: Tags are case sensitive. All tags must be closed. Attribute values must be enclosed in quotes. XML elements can have attributes that allow you to add information to an element that it does not contain. For example, in Listing 6-1, the BAND element has a TYPE attribute with a value of “ROCK”. XML tags are very similar to HTML tags. The less-than (<) and greater-than (>) symbols are used to delimit tags and the forward slash (/) is used to indicate closing tags. Elements are building blocks of an XML document. Every element in an XML document, with the exception of the document element, is a child element. Child elements can contain one of four content types: Element content Character content Mixed content Empty In our example, the and elements contain element content. All others contain character content. All elements in an XML document are nested, which gives the document a hierarchical tree appearance. If you’ll notice in the example, all of elements’ sub-elements are indented. The rules for nesting are strictly enforced. XML elements can also have attributes. For example: In the previous example, the element has an attribute named TYPE that is used to indicate what kind of music the band plays. Notice that the attribute value is enclosed in quotes, which are required. You can create attributes to help describe your elements. You could have also used another child element called rather than using an attribute. Either way is fine. It’s really a matter of preference. XML and the .NET Framework The important thing to remember about XML is that it is simply a way of describing data so that any application that knows the structure of the document can use it. Why do you need to know about XML when dealing with ASP.NET? Well, much of the .NET Framework revolves around the concept of universal data and application access. In fact, pretty much everything in the .NET world revolves around the premise of universal access. Probably two of the most evident examples of XML usage in ASP.NET are the config.web and global.asax files. Without going into too much detail about these files, they store application and configuration data. Here is an example of a config.web file:
From your knowledge of XML, you can see that the document element for the config.web file is . The document element contains several child elements including , , and . The element has an attribute named timeout that has a value of 120. The element is empty, which means that it does not contain any character data. When an element is empty is must be closed with the /> or syntax. The same could have been accomplished by writing: XML is also used in ASP.NET with Web Services. A Web Service’s results are always returned in XML format. Suppose you created a Web Service named Math that has a method named Add that sums two numbers. If you invoke the Web Service and call the Add function by passing 2 and 6 as the parameters you might get the following result:
8 You’ll notice that the result is returned in XML format. The XML document contains a prolog and a document element. That’s it.
You have probably heard a great deal about eXtensible Markup Language (XML) over the past few years. XML is on its way to becoming the de facto language for communications between devices, Web browsers, computers, servers, and applications. In time, any two applications will be able to exchange information without ever having been designed to talk to each other. In many ways, XML is just another file format—one more way to store information. However, XML as a file format is just the beginning. XML promises to liberate information from proprietary file formats and make it possible for information to move among multiple programs on different types of computers without facing the battery of conversion programs and lost information that is currently necessary. XML promises to dramatically increase both the efficiency and flexibility of the ways in which you handle information. In doing so, XML will have an impact on the way in which you use computers; it will change the way you look at applications. Fundamentally, XML makes it easy to store information in a hierarchical format, providing a consistent, easy-to-parse syntax and a set of tools for building rules describing the structure used to contain information. The XML format can represent both simple and complex information, and allows developers to create their own vocabularies for describing that information. XML documents can describe both themselves and their content. The XML Design Specs When you think of an “application,” you tend to think of a Web application — or a desktop application like Word or Excel. However, the creators of XML were a little less nearsighted many different kinds of applications. For this reason, the creators of XML established the following design commandments for the XML specification: 1. XML shall be straightforwardly usable over the Internet. This does not mean that XML should only be used over the Internet, but rather that it should be lightweight and easily usable over the Internet. 2. XML shall support a wide variety of applications. The idea here is that XML should not be application specific. It can be used over the Internet or in a traditional client/server application. There is no specific technology behind XML, so any technology should be able to use it. 3. It shall be easy to write programs that process XML documents. Unable to gain wide acceptance for various reasons, many technologies come and go. A major barrier to wide acceptance is a high level of difficulty or complexity. The designers of XML wanted to ensure that it would gain rapid acceptance by making it easy for programmers to write XML parsers. 4. XML documents should be human-legible and reasonably clear. Because XML is text-based and follows a strict but simple formatting methodology, it is extremely easy for a human to get a true sense of what a document means. XML is designed to describe the structure of its contents. 5. XML documents shall be easy to create. XML documents can be created in a simple text-editor. Now that’s easy! There are other XML guidelines, but since this only is an introduction to XML, these will do for now. The important thing to remember is that XML is simply a file format that can be used for two or more entities to exchange information. XML documents are hierarchical: they have a single (root) element, which may contain other elements, which may in turn contain other elements, and so on. Documents typically look like a tree structure with branches growing out from the center and finally terminating at some point with content. Elements are often described as having parent and child relationships, in which the parent contains the child element. The Structure of XML Documents XML documents must be properly structured and follow strict syntax rules in order to work correctly. If a document is lacking in either if these areas, the document can’t be parsed. There are two types of structures in every XML document: logical and physical. The logical structure is the framework for the document and the physical structure is the actual data. An XML document may consist of three logical parts: a prolog (optional), a document element, and an epilog (optional). The prolog is used to instruct the parser how to interpret the document element. The purpose of the epilog is to provide information pertaining to the preceding data. Listing 6-1 shows the basic structure of an XML document. Listing 6-1 Basic structure of an XML document
Hootie And The Blowfish Darius Rucker
Dean Felber
Mark Bryan
Jim Sonefeld
The prolog is made up of two parts: the XML declaration and an optional Document Type Declaration (DTD). The XML declaration identifies the document as XML and lets the parser know that it complies with the XML specification. Although the prolog, and thereby the XML declaration, is optional, we recommend that you include them in all your XML documents. Here is an example of a simple XML declaration:
The XML declaration can also contain more than just the version attribute. Some of the more important ones are the encoding and standalone attributes. The document type declaration establishes the grammar rules for the document or it points to a document where these rules can be found. The DTD is optional, but, if included, must appear after the XML declaration. XML documents can also reference a Schema rather than a DTD. Schemas perform essentially the same function as DTDs, but can describe more complex data types and are actually XML documents themselves. When possible, we recommend using a Schema rather than a DTD as Schemas are quickly becoming the de-facto standard for describing XML documents. An XML document is referred to as well formed when it conforms to all XML syntax rules. A valid XML document follows the structural rules defined in a Document Type Definition or Schema. All the data in an XML document is contained within the document element (in this example, ). You can’t have more than one document element in the same document, but the document element can contain as many child elements as necessary.
CREATE TABLE [dbo].[t_albums] ( [album_id] [int] IDENTITY (1, 1) NOT NULL , [album_title] [varchar] (255) NOT NULL , [album_publish_date] [datetime] NOT NULL , [band_id] [int] NOT NULL , [album_price] [smallmoney] NOT NULL ) ON [PRIMARY] GO ALTER TABLE [dbo].[t_albums] WITH NOCHECK ADD CONSTRAINT [DF_t_albums_album_publish_date] DEFAULT (getdate()) FOR [album_publish_date], CONSTRAINT [DF_t_albums_album_price] DEFAULT (0.00) FOR [album_price], CONSTRAINT [PK_t_albums] PRIMARY KEY NONCLUSTERED ( [album_id] ) ON [PRIMARY] , CONSTRAINT [IX_band_albums] UNIQUE NONCLUSTERED ( [album_title], [band_id] ) ON [PRIMARY] GO ALTER TABLE [dbo].[t_albums] ADD CONSTRAINT [FK_t_albums_t_bands] FOREIGN KEY ( [band_id] ) REFERENCES [dbo].[t_bands] ( [band_id] ) GO Everything here should look familiar. These commands are very similar to those used to create the t_bands table. The only difference is the last command that creates the foreign key to the band_id column in the t_bands table. That’s it for tables. Now try creating the rest of the database on your own. If you run into problems, feel free to use the T-SQL statements that are included on the CD. Next, we take a quick look at the views, stored procedures, and triggers for SQL Server database objects.
Creating a Database The first step in building a database with SQL Server is to actually create the database. That’s right. SQL Server is a piece of software that runs on a computer, or server. Once the SQL Server software is installed you can create a database (or databases) with the SQL Server software database, which it is, sort of. SQL Server is actually an application, a Relational Database Management System (RDBMS), which can contain multiple databases. We will be using SQL Server 7.0 to create the database in this session. If you are using SQL Server 2000, the steps will be slightly different. OK, let’s create the Music database. You’ll start by creating the database using Enterprise Manager and perform the following steps: 1. Expand the SQL Server Group item, if it isn’t already expanded, in the Enterprise Manager tree. Once expanded you should see a list of SQL Servers that are registered with Enterprise Manager. 2. Right-click the SQL Server in which you want to create the Music database. 3. Select New➪Database. Figure 4-1 illustrates steps 1, 2, and 3. 4. You see the Database Properties dialog box, shown in Figure 4-1. On the General tab, enter Music in the Name field. The Database Properties dialog box allows you to control other features of your database such as file growth, maximum database size, transaction log files, and so on. For the sake of brevity, accept the defaults. Specifying database properties with Enterprise Manager That’s it. You have created a SQL Server database using Enterprise Manager. If you want to create a database with T-SQL, follow these steps: