XML Syntax

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.

0 comments: