Home

Characteristics of XML Nodes

 

The Structure of an XML Node

 

Introduction

A seen in the previous lessons, XML is used to describe data using objects called nodes that are arranged as an upside-down tree with a root element, optional branches, and optional leaves. Consider the following 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>

The nodes of an XML file in the .NET Framework are supported through the XmlNode class.

The Types of Nodes

Because an XML file can have different kinds of nodes, to be able to identify them, each node is recognized by its specific type. The types of node of an XML file are stored in the XmlNodeType enumerator. We will review them as we move on.
 

The Children of a Node

 

Introduction

In our introduction to XML, we saw that a node can be nested inside of another. When a node is nested, 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. The child nodes of a particular node are accessed through the ChildNodes property of the XmlNode class. This property is defined as follows:

Public Overridable ReadOnly Property ChildNodes As XmlNodeList

When this property is used, it produces an XmlNodeList list, which is a collection of all nodes that share the same parent. The combined text of the values of the children of a node is available through its XmlNode.InnerText property which is declared as follows:

Public Overridable Property InnerText As String

This property concatenates the values of the children of the node that called them and doesn't include their markups. If you want to get a node, its markup, its child(ren) and its(their) markup(s), you can access its XmlNode.OuterXml property which is declared as follows:

Public Overridable ReadOnly Property OuterXml As String

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

Public Overridable Property InnerXml As String

The Parent of a Node

Not all nodes have children, obviously. For example, the Title node in the above example doesn't have children. To find out whether a node has children, check its HasChildNodes Boolean property that is declared as follows:

Public Overridable ReadOnly Property HasChildNodes As Boolean

If a node is a child, to get its parent, you can access its ParentNode property.

The First Child Node

The children of a nesting node are also recognized by their sequence. Based on this, the first node that immediately follows the start-tag of the parent node is called the first child. In the case of the above file, the first <Video> node under <Videos> is the first child of the Video node. In the .NET Framework, the first child of a node can be retrieved by accessing the XmlNode.FirstNode property declared as follows:

Public Overridable ReadOnly Property FirstChild As XmlNode

The Last Child Node

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 Siblings of a Node

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 XmlNode.NextSibling property, which is declared as follows:

Public Overridable ReadOnly Property NextSibling As XmlNode

Overview of Types of Nodes

 

Introduction

To differentiate the various nodes that belong to an XML file, they are classified by their category. As mentioned earlier, the types of node are listed in the XmlNodeType enumerator.

 

Comments

A comment is a character, a line or a paragraph that is not considered as part of the XML code that needs to be processed. A comment allows you to insert notes or personal observations inside an XML file. For this reason, a commented section can be written any way you like. This means that a comment can include plain text, formulas, expressions, or even XML code as long as you know that that XML code will not be validated: it will ignored by the parser.

To create a comment, you use the following formula:

<!-- Blah Blah Blah -.

Between <!-- and -., any text in that section is considered a comment and you can include anything you want. Both sections of the comment use two dashes, not more, not less. Here is an example:

<?xml version="1.0" encoding="utf-8"?>
<Videos>
  <Video>
	<!-- In this collection, we will keep each title "as i" -.
	<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>

This would produce:

The System.Xml represents a comment through the XmlComment class. Like any other part of an XML file, a comment is represented by the XmlComment.Name property. This allows you to retrieve the name of a comment that is included in the document.

To create a comment, you can call the XmlDocument.CreateComment() method. Its syntax is:

Public Overridable Function CreateComment(ByVal data As String) As XmlComment

This method takes as argument the text that would go into the commented section. After calling it, if the method succeeds, which it usually does, it returns the XmlComment object that was created.

CDATA

Except for comments, the parser is used to "scan" the whole XML file to analyze it. Every tag is then interpreted. As we mentioned already, the value of each tag can be displayed in a browser between its opening and its closing tag, and the browser uses different font styles to make a distinction. When creating some tags and some sections of the file, you may want the parser to consider those particular tags and sections as regular text. That is, you may want the parser to treat a certain tag and its value as if it were regular text even though it is created as an XML file.

To prevent the parser from interpreting a tag regularly but to treat that tag and its value as regular text, you can create it in a CDATA section. To do this, create a section that starts with <![CDATA[, followed by anything you want, and ending with ]]>. The formula used is:

<![CDATA[ Blah Blah Blah ]]>

Between <![CDATA[ and ]]>, you can type anything, including one or more normal XML tags. Here is an example:

<?xml version="1.0" encoding="utf-8"?>
<Videos>
  <![CDATA[<VideoCollection>Personal Collection of Movies</VideoCollection>]]>
  <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>

The .NET Framework supports the creation of a CDATA section through the XmlCDataSection class. This class is equipped with a Name property that allows you t retrieve the name of a CDATA section in an XmlDocument object.

To programmatically create a CDATA section, you can call the XmlDocument.CreateCDataSection() method. Its syntax is:

Public Overridable Function CreateCDataSection(ByVal data As String) As XmlCDataSection

This method receives the content of the CDATA section as argument. If the method succeeds, which it usually does, it returns an XmlCDataSection value. 

Operations on Nodes

 

Simple Node Addition

To add a new node to an XML file, the XmlNode class provides various methods. To start, an XML file must have a root. This ensures that the file has at least one node. Before adding a new node, you must have a reference of another node. This information will allow you to decide where to position the new node.

To add a new node as a child of an existing node, the simplest position to use is to add the new node at the end of the list of nodes of the existing node. This position is supported by the XmlNode.AppendChild() method. Its syntax is:

Public Overridable Function AppendChild(ByVal newChild As XmlNode) As XmlNode

This method accepts as argument the new node that will be created. This means that you can first "build" an XmlNode object. To do this, you can use a pointer to the type of node you want to create.
 

Node Addition to Sibling 

Instead of simply adding a new node at the end of child nodes, you can specify any other position you want. For example, you may want the new node to precede an existing child node. To support this operation, the XmlNode class provides the InsertBefore() method. Its syntax is:

Public Overridable Function InsertBefore(ByVal newChild As XmlNode, _
                                                                 ByVal refChild As XmlNode) As XmlNode

The first argument of this method is the new node that will be added. The second argument is the sibling that will succeed the the new node. If you want to new node to be positioned after an existing child node, you can call the XmlNode.InsertAfter() method. Its syntax is:

Public Overridable Function InsertAfter(ByVal newChild As XmlNode, _
                                                                 ByVal refChild As XmlNode) As XmlNode

 

Node Removal

If you have a node you don't want or don't need anymore in the file, you can delete it. To delete a node, the XmlNode class provides the RemoveChild() method. Its syntax is:

Public Overridable Function RemoveChild(ByVal oldChild As XmlNode) As XmlNode

This method takes as argument the node to delete. If the node exists, it would be deleted and the method would return it. If the node doesn't exist, nothing would happen.

To delete all child nodes of a node, you can call the XmlNode.RemoveAll() method. Its syntax is:

Public Overridable Sub RemoveAll()

When called, this method will remove all child nodes, if any, of their parent node. 

 

 

Previous Copyright © 2004-2010 FunctionX, Inc.