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 LearningPractical Learning: Introducing XML Nodes

  1. Create a new Combine as a Windows Application named CPAP2
  2. To create a new XML file, on the main menu, click File -> New -> File...
  3. In the Categories list of the New File dialog box, click Misc
  4. In the Templates list, click Empty XML File and click Create
  5. To save the file, on the main menu, click File -> Save
  6. Display the folder that contains the current project (CPAP2). Double-click its bin followed by its Debug sub-folders
  7. Make sure the Save As Type combo box displays Xml Files (*.xml).
    Set the file name to Makes.xml
  8. Complete the file as follows:
    <?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>
  9. To create a new XML file, on the main menu, click File -> New -> File...
  10. In the Categories list of the New File dialog box, click Misc
  11. In the Templates list, click Empty XML File and click Create
  12. To save the file, on the main menu, click File -> Save
  13. Display the Debug sub-folder of the bin folder inside the folder of the current project (CPAP2) and make sure the Save As Type combo box displays Xml Files (*.xml).
    Set the file name to Models.xml and click Create
  14. Complete the file as follows:
    <?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>
  15. Create a new XML file. Save it as Parts.xml in the bin\Debug sub-folder of the current project
  16. Complete the file with a few parts:
    <?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>
  17. Add another XML file to the project and name it Employees.xml
  18. Complete it 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. Save all

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 of 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 virtual string InnerText {get; set; }

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

public virtual string InnerXml {get; set; }

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 virtual XmlNode LastChild {get;}

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 virtual XmlNode NextSibling {get;}
 

Previous Copyright © 2005-2016, FunctionX Next