Sams Teach Yourself Visual Studio .NET 2003 in 21 Days

Sams Teach Yourself .Net in 21 Days

By Jason Beres

What Is XML?

XML is a text-based way to describe structured data. Unlike HTML, which uses various markup tags to describe how data should be displayed or rendered in a Web browser, XML is simply data. With the need to move data between systems in a flexible and platform-neutral way, XML has become the standard way to pass data around the Internet. Some of the reasons for this are

The data in an XML document is described through the use of elements. Elements create the structure for the document, similar to column names in a database. Within the element tags of an XML document lies the actual data. The element name describes the data in the element. An XML document can also contain attributes, which further describe the data contained in an element. Listing 12.1 is an example of a simple XML document.

Example 12.1. Simple XML Document

<?xml version="1.0"?>
<!-- This simple data represents food items in a grocery store -->
<GroceryStore>
        <StoreName>Brian's Groceries</StoreName>
        <Departments>
                <Department Name="Breads">
                        <Item ID="B1">
                                <Name>Wonder Bread</Name>
                                <Price>1.29</Price>
                                <New />
                        </Item>
                        <Item ID="B2" Type="Muffin">
                                <Name>Blueberry Muffin</Name>
                                <Price>3.99</Price>
                                <New />
                        </Item>
                </Department>
                <Department Name="Fruits">
                        <Item ID="F1">
                                <Name>Apple</Name>
                                <Price>0.99</Price>
                        </Item>
                </Department>
        </Departments>
</GroceryStore>

The XML document can be broken down into several key parts.

The prolog of an XML document defines the XML version number and encoding information. This is required for an XML document to be well formed. In Listing 12.1, the prolog is

<?xml version="1.0"?>
<!-- This simple data represents food items in a grocery store -->

The prolog can contain

The root element of the XML document is required. It's the main parent in the XML tree for the document. The <GroceryStore> beginning element and </GroceryStore> end element define the root element for Listing 12.1.

Tags define the boundary of the data content within an XML document. In an XML document, there are start tags, end tags, and empty tags. The start tag defines the element name, and everything between the start tag and end tag can be considered the data for the element. An empty tag contains no data, but it can contain attributes. Attributes are name-value pairs that can be used to further describe the data within an element.

In Listing 12.1, the Department element (with start tag <Department> and end tag </Department>) contains Item child elements, which also contain Name and Price child elements. The Department element has a Name attribute, and the Item element has an ID attribute and a Type attribute. The first two Items for the Department Bread have empty elements named New.

As Listing 12.1 demonstrates, XML documents use a tree-like hierarchy to describe the data they contain. Using the tools and namespaces in .NET, you can quickly and easily parse XML documents such as Listing 12.1.

Share ThisShare This

Informit Network