Getting Started with XML

IOTA^_^    

ASP.NET Developer's JumpStart
By Paul D. Sheriff, Ken Getz
Table of Contents
Chapter 27.  Introduction to XML


In its simplest sense, XML is a markup language that uses tags to describe elements, much like HTML. Unlike HTML, however, XML doesn't contain any predefined tags in XML, you make up your own tags to describe your data.

Take a look at some simple XML data, as shown in Listing 27.1. (You can find this content in Jumpstart\XMLIntro\EmployeeData.xml.)

Listing 27.1 This Simple XML Document Shows off Many of the Important XML Syntax Features
 <?xml version="1.0" encoding="utf-8" ?> <!--  Employee Information -- > <EmployeeData>   <Employee >     <FirstName>Davolio</FirstName>     <LastName>Nancy</LastName>   </Employee>   <Employee >     <FirstName>Fuller</FirstName>     <LastName>Andrew</LastName>   </Employee>   <Employee >     <FirstName>Levering</FirstName>     <LastName>Janet</LastName>   </Employee> </EmployeeData> 

This example shows off a number of XML features, including the following:

  • XML declaration. The first line of text declares the version of XML you're using (generally 1.0, at this point) and other information about how the XML should be handled by the XML processor:

     <?xml version="1.0" encoding="utf-8" ?> 
  • Comments. You can include comments anywhere within the XML content, using the <!-- and -- > symbols:

     <!--  Employee Information -- > 
  • Tag syntax. Each element has a beginning and ending tag, which must match exactly, like this:

     <EmployeeData>. . . </EmployeeData> 
  • Attributes. Elements can contain one or more attributes, which are nothing more than name/value pairs in which the value must always be a string surrounded by quotes. The xml declaration element contains two attributes, and the Employee elements each contain one attribute:

     <?xml version="1.0" encoding="utf-8" ?> <Employee >. . . </Employee> 
  • Elements that contain other elements. The Employees element contains a group of Employee elements, each of which contains FirstName and LastName elements:

     <EmployeeData>   <Employee > . . .   </Employee>   <Employee > . . .   </Employee>   <Employee > . . .   </Employee> </EmployeeData> 
  • Elements that contain text. The FirstName and LastName elements each contain information:

     <FirstName>Davolio</FirstName> <LastName>Nancy</LastName> 

TIP

A few other lexical issues are involved in creating XML text, but not many. Any good text on XML will be able to help fill in the rest of the details.


XML Element Rules

XML is very strict about the names you choose for your elements. Here are some specifics:

  • Tags can contain letters, numbers, and colons (:).

  • Tags must contain at least one letter.

  • Tags must begin with a letter or underscore.

  • Tag names cannot contain spaces but can include hyphens and periods.

  • Most important, tag names are case sensitive.

This last rule is hard to get used to, especially when you're used to working in a non case-sensitive world.

Normally, elements contain data or other elements. If you have an element with neither, it can appear either like

 <EmptyElement></EmptyElement> 

or, equivalently, like this:

 <EmptyElement/> 

TIP

Beginning XML developers often ask, "How can I tell whether I should place information in elements or in attributes?" Although you're unlikely to be creating raw XML at this point, you still may be wondering how to best structure XML. Here's a simple answer: Think of XML elements as the data, and attributes as properties of the data. That's a nebulous distinction, but it helps to consider the issue this way. You might think of each element as an object, and the attributes of the element as its properties.


XML Document Structure

Every XML document should contain the same basic contents: the XML declaration, a single root element, and elements and attributes, as necessary. If you look carefully at the code shown in Listing 27.1, you'll find all three of these items:

  • The XML declaration:

     <?xml version="1.0" encoding="utf-8" ?> 
  • The root element:

     <EmployeeData> . . . </EmployeeData> 
  • Elements and attributes:

     <Employee >   <FirstName>Davolio</FirstName>   <LastName>Nancy</LastName> </Employee> <Employee >   <FirstName>Fuller</FirstName>   <LastName>Andrew</LastName> </Employee> 

We used the term well-formed on purpose. When you describe XML data as being "well-formed," you're indicating that the content follows all the XML structure rules. This is the minimum requirement for any XML document if the content isn't well-formed, there's no chance that any XML processor will be able to consume your XML. Although the XML version declaration isn't required, a well-formed document will have only a single root element, and all elements will follow the XML syntax rules.

TIP

XML elements must be nested completely. That is, you cannot begin element A, begin element B, end element A, and then end element B. For this content to be well-formed, you must end element B before you end element A. In other words, the following isn't well-formed:

 <A><B></A></B> 

However, this is:

 <A><B></B></A> 


graphics/tryitout.gif

Although there are many tools you can purchase to help you work with XML data, Visual Studio .NET provides very solid, basic XML-handling functionality built in. Follow these steps to use Visual Studio .NET to load and check the "well-formedness" of an XML document (well-formedness sounds awkward, but it's really the correct terminology for describing a well-formed XML document):

  1. Load the Northwind.sln solution you've been working on throughout this book.

  2. Select the Project, Add Existing Item menu item and browse to the Jumpstart\XMLIntro folder.

  3. With the Files of Type drop-down list set to "All Files (*.*)," select EmployeeData.xml. Click Open to add the file to your project.

  4. In the Solution Explorer window, double-click the EmployeeData.xml item to load it into the XML editor, as shown in Figure 27.1.

    Figure 27.1. You can load XML data directly into a Visual Studio .NET project.

    graphics/27fig01.jpg

  5. In addition to the standard XML view you see in Figure 27.1, you can also use Visual Studio .NET's data editor to view the XML. Click the Data tab at the bottom of the window to switch to Data view, as shown in Figure 27.2.

    Figure 27.2. Use Data view to see a more traditional view of your XML data.

    graphics/27fig02.jpg

  6. You can use the data editor to modify, add, and delete rows of data from your XML file. Try adding a new row for employee 4, Margaret Peacock. When you're done, switch back to XML view and verify that Visual Studio .NET added the new row to the XML source.

  7. To test what happens if your XML isn't well-formed, once you're back in XML view, try changing the name of a tag so that it doesn't match its ending tag. For example, try changing the beginning <Employee> tag for Margaret Peacock to <Employees> instead.

  8. Switch back to Data view, and you'll see an error message like the one shown in Figure 27.3.

    Figure 27.3. XML that isn't well-formed can't be displayed in Data view.

    graphics/27fig03.jpg

  9. Switch back to XML view and repair your change so that the data works correctly.

  10. Try viewing the XML in Internet Explorer (which contains support for displaying XML natively). In the Solution Explorer window, right-click EmployeeData.xml and select View in Browser from the context menu. If you've correctly fixed the XML syntax so that the content is well-formed, you should see the browser window shown in Figure 27.4.

    Figure 27.4. Internet Explorer can display formatted XML.

    graphics/27fig04.jpg


    IOTA^_^    
    Top


    ASP. NET Developer's JumpStart
    ASP.NET Developers JumpStart
    ISBN: 0672323575
    EAN: 2147483647
    Year: 2002
    Pages: 234

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