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

  • XML is plain text; any computer can understand plain text

  • Data in an XML file is self-describing

  • Parsing XML documents has become fairly simple

  • XML can be transmitted over HTTP

  • XML can work across firewalls over HTTP

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.

Listing 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

  • XML comments— XML comments are defined by the <!-- and --> start and end tags. XML comments can be in the prolog or anywhere else within the XML document.

  • Namespace declaration— The XML namespace for the current document in the prolog. Like namespaces in .NET, an XML namespace can be used to uniquely identify the contents of the document. In .NET, you can create XML software definition (XSD) files that contain schema information for an XML file.

  • XML stylesheet declaration— An XML stylesheet contains information about how the data in the document should be displayed. A stylesheet can contain HTML tags or application logic used to transform the XML document structure to another XML document structure.

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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.22.74.3