Two advantages of the XML format are that humans can read it and there are many tools for manipulating it. To pave the way to the following chapter, let’s examine the structure of an XML document and look briefly at the tools for editing XML documents.
Switch to the XMLBooks project and add the XML file with the serialized books to the project or to a new Windows project. Right-click the name of the project and from the context menu choose Add Existing Item to open the Add Existing Item dialog box. Select Data Files in the File Type combo box and move up to the project’s main folder. Select the SerializedXMLArray.xml file and add it to the project by selecting the Add option from the Add drop-down list on the dialog box.
Then double-click the newly added file to open it in the editor, as shown in Figure 12.4. The XML editor will figure out the structure of the document and indent the file’s contents. XML files are made up of tags, which are embedded in angle brackets. Each tag has a name, and the tags go in pairs: the opening tag and the closing tag. Everything between an opening and closing tag is an element. As you can see, elements may contain other elements, to any depth. In fact, the entire document (with the exception of its header) is enclosed in a pair of <ArrayOfBook> and </ArrayOfBook> tags. This is the document’s root element, and a valid XML file should have a single root element that encloses all other elements. Moreover, the remaining elements must nest properly; each element must be closed under the parent element in which it was opened, just like nesting flow control structures in VB.
The first line in the file is the document’s prologue, and it identifies an XML file. The version keyword identifies the version of the XML specification. Note that the prologue tag has no closing tag.
The document’s body is embedded in the ArrayOfBook element. This is the document’s root element, and each XML document must have one, and only one, root element.
Each entity is mapped to an element, and each element may have zero or more subelements. An XML file with customers should probably have a <Customers> root element, which in turn should contain any number of <Customer> elements. Each <Customer> element is made up of more-specific elements, such as <Name>, <Address>, <Email>, and so on. The names of the tags make sense to us humans, but to the computer, they’re just names. Computers can’t manipulate an XML document based on the names of the elements; they can understand only the structure of the document. For example, you can request the total number of elements in the document, the child elements of a specific element, the Email element of a specific Customer element, and so on.

Each element may have one or more attributes. Instead of specifying all the items as elements, we can specify them as attributes of the element to which they belong. Instead of nesting the LastName and FirstName elements under the Author element, you can specify the LastName and FirstName fields as attributes:
<Author FirstName="author First" LastName="author Last"/>
Code language: HTML, XML (xml)
Using attributes in place of elements is just a way to organize the information in an XML document. Using attributes doesn’t change the nature of the document, just its structure.
You can actually determine how the XmlSerializer class will create the XML file with the proper attributes. Say you modify the declaration of the Authors class in the Books sample project by prefixing the FirstName and LastName properties with the <XmlAttribute> attribute, as shown here:
<Serializable()> Public Class Author
<XmlAttribute(AttributeName:="FirstName")> _
Public Property FirstName() As String
Get
Return_firstname
End Get
Set(ByVal Value As String)
If Value.Length > 50 Then
_firstname = Value.Substring(0, 49)
Else
_firstname = Value
End If
End Set
End Property
<XmlAttribute(AttributeName:="LastName")> _
Property LastName() As String
Get
Return _lastname
End Get
Set(ByVal Value As String)
If Value.Length > 50 Then
_lastname = Value.Substring(0, 49)
Else
_lastname = Value
End If
End Set
End Property
Code language: JavaScript (javascript)
The XmlSerializer will insert the author’s first and last names as attributes of the <Author> tag. Here’s an <Author> tag generated by the XmlSerializer with the revised class definition:
<Authors>
<Author FirstName="Author1 First" LastName="Author1 Last" />
<Author FirstName="Author2 First" LastName="Author2 Last" />
</Authors>
Code language: HTML, XML (xml)
There are other attributes for customizing the output of the XmlSerializer, but a discussion of these attributes is beyond the scope of this tutorial.