Home

Introduction to XML Elements

 

Elements Fundamentals

 

Introduction

An element in an XML document is an object that starts with a start-tag, may contain a value, and may end with an end-tag. Based on this, the combination of a start-tag, the value, and the end-tag is called an element. An element can be more than that but for now, we will consider that an element is primarily characterized by a name and possibly a value.

To support XML elements, the System.Xml namespace provides the XmlElement class.

The Name of an Element

The name of an element is the string that represents the tag. For example, in <Director>, the word Director is the name of the element. An element must have at least a start-tag. All of the tags we have seen so far were created as elements. When creating your elements, remember to follow the rules we defined for names.

The XmlElement class is equipped with the Name property that can be used to identify an existing element.

The Text or Value of an Element

The value of an element is the item displayed on the right side of the start-tag. It is also called the text of the element. In the case of <Director>Jonathan Lynn</Director>, the "Jonathan Lynn" string is the value of the Director element. To support the text or value of an element, the XmlElement class is equipped with the Value property.

While the value of one element can be a number, the value of another element can be a date. Yet another element can use a regular string as its value. Consider the following example:

<?xml version="1.0" encoding="utf-8" ?>
<Employees>
	<Employee>
		<FullName>Lydia Thomason</FullName>
		<Salary>25.64</Salary>
		<DepartmentID>1</DepartmentID>
	</Employee>
	<Employee>
		<FullName>June Grath</FullName>
		<Salary>16.38</Salary>
		<DepartmentID>4</DepartmentID>
	</Employee>
</Employees>

Notice that the Salary elements contain numbers that look like currency values and the DepartmentID elements use an integer as value.

The value or text of an element is an object of type XmlText.

 

Practical Learning Practical Learning: Introducing XML Nodes

  1. Start Notepad and, in the empty file, type the following:
     
    <?xml version="1.0" encoding="utf-8"?>
    <Makes>
      <Make>Acura</Make>
      <Make>Alfa Romeo</Make>
      <Make>Audi</Make>
      <Make>BMW</Make>
      <Make>Chevrolet</Make>
    </Makes>
  2. To start a new file, on the main menu, click File -> New
  3. When asked whether you want to save the changes, click Yes
  4. Locate your VBasic folder and display it in the Save In combo box
  5. Click the Create new Folder button
  6. Type CPAP1 as the name of the new folder and press Enter twice to display it in the Save In combo box
  7. Set the Save As Type combo box to All Files.
    Set the file name to Makes.xml and click Save
  8. In the new empty document, type the following:
     
    <?xml version="1.0" encoding="utf-8"?>
    <Models>
      <Model>NSX</Model>
      <Model>TL</Model>
      <Model>Spider</Model>
      <Model>A4</Model>
      <Model>RS6</Model>
      <Model>323I</Model>
      <Model>M5</Model>
      <Model>Astro</Model>
      <Model>Cavalier</Model>
    </Models>
  9. To start a new file, on the main menu, click File -> New
  10. When asked whether you want to save the changes, click Yes
  11. Make sure your CPAP1 folder displays in the Save In combo box, otherwise select it
  12. Set the Save As Type combo box to All Files.
    Set the file name to Models.xml and click Save
  13. Complete the new empty document with the following:
     
    <?xml version="1.0" encoding="utf-8"?>
    <Parts>
    	<Part>
    		<PartNumber>293749</PartNumber>
    		<CarYear>2005</CarYear>
    		<Make>Acura</Make>
    		<Model>MDX 3.5 4WD</Model>
    		<PartName>Air Filter</PartName>
    		<UnitPrice>16.85</UnitPrice>
    	</Part>
    	<Part>
    		<PartNumber>283759</PartNumber>
    		<CarYear>2002</CarYear>
    		<Make>Audi</Make>
    		<Model>A4 Quattro</Model>
    		<PartName>Clutch Release Bearing</PartName>
    		<UnitPrice>55.50</UnitPrice>
    	</Part>
    	<Part>
    		<PartNumber>491759</PartNumber>
    		<CarYear>1998</CarYear>
    		<Make>Dodge</Make>
    		<Model>Neon</Model>
    		<PartName>Crankshaft Position Sensor</PartName>
    		<UnitPrice>22.85</UnitPrice>
    	</Part>
    	<Part>
    		<PartNumber>844509</PartNumber>
    		<CarYear>2000</CarYear>
    		<Make>Chevrolet</Make>
    		<Model>Camaro</Model>
    		<PartName>Control Module Connector</PartName>
    		<UnitPrice>25.65</UnitPrice>
    	</Part>
    </Parts>
  14. To start a new file, on the main menu, click File -> New
  15. When asked whether you want to save the changes, click Yes
  16. Make sure your CPAP1 folder displays in the Save In combo box.
    Set the Save As Type combo box to All Files
  17. Set the file name to Parts.xml and click Save
  18. Complete the empty document as follows
     
    <?xml version="1.0" encoding="utf-8" ?>
    <Employees>
    	<Employee>
    		<EmplNumber>72-448</EmplNumber>
    		<FirstName>Sylvie</FirstName>
    		<LastName>Aronson</LastName>
    		<salary>25.64</salary>
    	</Employee>
    	<Employee>
    		<EmplNumber>84-502</EmplNumber>
    		<FirstName>Bertrand</FirstName>
    		<LastName>Yamaguchi</LastName>
    		<salary>16.38</salary>
    	</Employee>
    	<Employee>
    		<EmplNumber>68-335</EmplNumber>
    		<FirstName>Anselme</FirstName>
    		<LastName>Bean</LastName>
    		<salary>22.82</salary>
    	</Employee>
    	<Employee>
    		<EmplNumber>20-972</EmplNumber>
    		<FirstName>Mauricette</FirstName>
    		<LastName>Thomas</LastName>
    		<salary>18.35</salary>
    	</Employee>
    	<Employee>
    		<EmplNumber>63-114</EmplNumber>
    		<FirstName>Hermine</FirstName>
    		<LastName>Gray</LastName>
    		<salary>12.75</salary>
    	</Employee>
    </Employees>
  19. To start a new file, on the main menu, click File -> New
  20. When asked whether you want to save the changes, click Yes
  21. Make sure your CPAP1 folder displays in the Save In combo box and set the Save As Type combo box to All Files
  22. Set the file name to Employees.xml and click Save
  23. Complete the empty document with the following:
     
    Imports System
    Imports System.xml
    
    Module Exercise
        
        Public Sub Main()
            
        End Sub
    
    End Module
  24. Save the file as Exercise.vb in the CPAP1 folder of your VBasic folder
 

Character Entities in an Element Value

Besides these obvious types of values, you may want to display special characters as values of elements. Consider the following example:

<?xml version="1.0" encoding="utf-8" ?>
<Employees>
	<Employee>
		<FullName>Sylvie <Bellie> Aronson</FullName>
		<Salary>25.64</Salary>
		<DepartmentID>1</DepartmentID>
	</Employee>
	<Employee>
		<FullName>Bertrand Yamaguchi</FullName>
		<Salary>16.38</Salary>
		<DepartmentID>4</DepartmentID>
	</Employee>
</Employees>

If you try using this XML document, for example, if you try displaying it in a browser, you would, receive an error:

An XML file in a browser

The reason is that when the parser reaches the <FullName>Sylvie <Bellie> Aronson</FullName> line, it thinks that <Bellie> is a tag but then <Bellie> is not closed. The parser concludes that the document is not well-formed, that there is an error. For this reason, to display a special symbol as part of a value, you can use its character code. There is a list of  parent of XML, provides a list of the characters for all special symbols. For example, the left angle bracket can be represented with &lt; while the right angle bracket can be represented with &gt;. Based on this, the above code can be corrected as follows:

<?xml version="1.0" encoding="utf-8" ?>
<Employees>
	<Employee>
		<FullName>Sylvie &lt;Bellie&gt; Aronson</FullName>
		<Salary>25.64</Salary>
		<DepartmentID>1</DepartmentID>
	</Employee>
	<Employee>
		<FullName>Bertrand Yamaguchi</FullName>
		<Salary>16.38</Salary>
		<DepartmentID>4</DepartmentID>
	</Employee>
</Employees>

This would produce:

XML also provides its own code to include special characters in your XML files.

Empty Elements

An element may not have a value but only a name. Consider the following example:

<?xml version="1.0" encoding="utf-8"?>
<Videos>
  <Video>
    <Title>The Distinguished Gentleman</Title>
    <Director>Jonathan Lynn</Director>
  </Video>
</Videos>

In this case, the Video element doesn't have a value. It is called an empty element but it is an element in its own right.

Overview of XML Nodes

 

Introduction

An XML file appears as an upside-down tree: it has a root, can have branches and leaves. As we have seen in the previous lesson, all of these objects are created using the same technique: a tag with a name and an optional value. Based on their similarities, these objects can be referred to as nodes. Here is an example:

<?xml version="1.0" encoding="utf-8"?>
<Videos>
  <Video>
    <Title>The Distinguished Gentleman</Title>
    <Director>Jonathan Lynn</Director>
    <Length>112 Minutes</Length>
    <Format>DVD</Format>
    <Rating>R</Rating>
  </Video>
  <Video>
    <Title>Her Alibi</Title>
    <Director>Bruce Beresford</Director>
    <Length>94 Mins</Length>
    <Format>DVD</Format>
    <Rating>PG-13</Rating>
  </Video>
</Videos>

To support nodes of an XML file, the .NET Framework provides the XmlNode class, which is the ancestor to all types of nodes.

As seen in the previous lesson, one node can be nested inside of another. In that case, the nested node is called a child of the nesting node, which is a parent node. This also implies that a node can have as many children as necessary, making them child nodes of the parent node. In the above example, the Title and the Director nodes are children of the Video node. The Video node is the parent of both the Title and the Director node.

Child Nodes

To support the child nodes of a particular node, the XmlNode class is equipped with the ChildNodes property. To get the whole text of the values of the children of a node, access its InnerText property which is declared as follows:

Public Overridable Property InnerText As String

If you want only the markup(s) of the child(ren) excluding the parent, access its InnerXml property which is declared as follows:

Public Overridable Property InnerXml As String

The children of a nesting node are also recognized by their sequence. Based on this, the first node is called the first child. As opposed to the first child, the child node that immediately precedes the end-tag of the parent node is called the last child. To get the last child of a node, you can access its XmlNode.LastChild property that is declared as follows:

Public Overridable ReadOnly Property LastChild As XmlNode

The child nodes that are nested in a parent node and share the same level are referred to as siblings. Consider the following listing:

<?xml version="1.0" encoding="utf-8"?>
<Videos>
  <Video>
	<Title>The Distinguished Gentleman</Title>
	<Director>Jonathan Lynn</Director>
	<Actors>
		<Actor>Eddie Murphy</Actor>
		<Actor>Lane Smith</Actor>
		<Actor>Sheryl Lee Ralph</Actor>
		<Actor>Joe Don Baker</Actor>
	</Actors>
	<Length>112 Minutes</Length>
	<Format>DVD</Format>
	<Rating>R</Rating>
  </Video>
  <Video>
	<Title>Her Alibi</Title>
	<Director>Bruce Beresford</Director>
	<Length>94 Mins</Length>
	<Format>DVD</Format>
	<Rating>PG-13</Rating>
  </Video>
</Videos>

In this example, Director, Actors, and Length are child nodes of the Video node but the Actor node is not a child of the Video node. Consequently, Director, Actors, and Length are siblings.

Obviously, to get a sibling, you must first have a node. Based on this, to access the sibling of a node, you can use its NextSibling property, which is declared as follows:

Public Overridable ReadOnly Property NextSibling As XmlNode

 

 

Previous Copyright © 2005-2012 FunctionX Next