Fundamentals of Attributes

Introduction

When studying XML elements, we saw how they constituted the main objects of an XML document. We also saw that an element could be nested inside another element. Instead of nesting an element, you can transform the nested element into being part of the nesting element and thereby giving away its element qualities. This is the basis of an attribute.

An attribute is a value that is created as part of an element, making that value different from the value of a regular element. There are similarities and differences between an element and an attribute.

The element and the attribute have these in common:

The differences between an element and an attribute are:

XML Attributes in .NET

To support XML attributes, the System.Xml namespace provides a class named XmlAttribute. Like all nodes, this class is based on the XmlNode class.

The Name of an Attribute

To let you get the name of an attribute, the XmlAttribute class is equipped with a (read-only) property named Name.

The Inner Text of an Attribute

To let you get the value of an attribute, the XmlAttribute class is equipped with a property named Value. Besides Value, to support the text of an attribute, the XmlAttribute class provides a property named InnerText.

The Inner XML of an Attribute

To give you access to the XML of an attribute, the XmlAttribute class is equipped with a property named InnerXml.

Creating an XML Attribute

Manually Creating an Attribute

Imagine you have an ISBN element as a child of a video element as follows:

<video>
  <ISBN>0-7888-1623-3</ISBN>
</video>

An attribute must be created inside the start-tag of an element. To manually create an attribute, type the left angle bracket of the element, followed by the name of the element, an empty space, and the name of the attribute. The name follows the same rules we defined for names in XML.

An attribute should have a value that can be used to distinguish it. To specify the name of an attribute, assign a value as a string to its name. In the case of the above code, since ISBN is simply a child of the video element, you can change the ISBN element to become an attribute of the video element as follows:

<video ISBN="0-7888-1623-3">

Now, ISBN is an attribute of the video element. If you are writing code, in the start tag of the desired element, specify the desired attribute and assign a single quoted value to it.

Adding an Attribute to an Element

As mentioned already, an attribute primarily belongs to an element. This means that, when creating an attribute, you must specify what element it would belong to. To support the attributes of an element, the XmlElement class is equipped with a method named SetAttribute. It is overloaded in two versions. The first version of this method uses the following syntax:

public virtual void SetAttribute(string name, string value);

The first argument is the name of the new attribute and the second argument will be its text. Consider an XML file named Employees.xml as follows:

<?xml version="1.0" encoding="utf-8" ?>
<employees>
  <employee>
    <employee-number>283947</employee-number>
    <first-name>Frank</first-name>
    <last-name>Euler</last-name>
    <hourly-salary>18.25</hourly-salary>
  </employee>
</employees>

To add an attribute to an element, create an XmlElement object and call the above method on it. Here is an example:

using System.Xml;

public class Exercise
{
    public static int Main(string[] args)
    {
        XmlDocument xdEmployees = new XmlDocument();
        string strEmployeesFile = @"C:\exercises\Employees.xml";

        xdEmployees.Load(strEmployeesFile);

        XmlElement xeEmployee = xdEmployees.CreateElement("employee");

        xeEmployee.InnerXml = "<employee-number>608504</employee-number>" +
                              "<first-name>Jeannette</first-name>" +
                              "<last-name>Ngo Koum</last-name>" +
                              "<hourly-salary>22.50</hourly-salary>";

        xdEmployees.DocumentElement.AppendChild(xeEmployee);

        xeEmployee.SetAttribute("status", "Full-Time");
        xdEmployees.Save(strEmployeesFile);

        return 111_111;
    }
}

This would produce:

<?xml version="1.0" encoding="utf-8"?>
<employees>
  <employee>
    <employee-number>283947</employee-number>
    <first-name>Frank</first-name>
    <last-name>Euler</last-name>
    <hourly-salary>18.25</hourly-salary>
  </employee>
  <employee status="Full-Time">
    <employee-number>608504</employee-number>
    <first-name>Jeannette</first-name>
    <last-name>Ngo Koum</last-name>
    <hourly-salary>22.50</hourly-salary>
  </employee>
</employees>

Add an Attribute to the Root

Consider an XML document as follows:

<?xml version="1.0" encoding="utf-8"?>
<videos>
</videos>

To add an attribute to the root node, call the SetAttribute() method on it. Here is an example:

using System.IO;
using System.Xml;

public class Exercise
{
    public static int Main(string[] args)
    {
        XmlDocument xdVideos = new XmlDocument();
        string strVideosFile = @"C:\Videos Collection\Videos.xml";
        FileInfo fiVideos = new FileInfo(strVideosFile);

        if( fiVideos.Exists == true )
        {
            // Open the XML file
            xdVideos.Load(fiVideos.FullName);

            // Create an attribute and add it to the root element
            xdVideos.DocumentElement.SetAttribute("FileDesc",
                                                  "Personal Video Collection");
            xdVideos.Save(fiVideos.FullName);
        }

        return 222_222;
    }
}

From the above videos.xml file, this code would result in:

<?xml version="1.0" encoding="utf-8"?>
<videos FileDesc="Personal Video Collection">
</videos>

Adding an Attribute to an Existing Element

Consider an XML file named Videos.xml that has the following content:

<?xml version="1.0" encoding="utf-8"?>
<videos FileDesc="Personal Video Collection">
  <video ISBN="0-7888-1623-3">
    <title Screenplay="Marty Kaplan">The Distinguished Gentleman</title>
    <director>Jonathan Lynn</director>
    <actors>
    </actors>
    <length>112 Minutes</length>
    <format>DVD</format>
    <rating>R</rating>
  </video>
  <video>
    <title WrittenBy="Charlie Peter">Her Alibi</title>
    <director>Bruce Beresford</director>
    <length>94 Mins</length>
    <format>DVD</format>
    <rating>PG-13</rating>
  </video>
</videos>

To support attribute addition, the XmlDocument class is equipped with a method named CreateAttribute. It is overloaded with three versions. The first version of this method has the following syntax:

public XmlAttribute CreateAttribute(string name);

This method expects the name of the attribute as argument. If it succeeds, this method produces an XmlAttribute object. To let you add the new attribute to an element, the XmlElement class is equipped with a method named SetAttributeNode method. This method is overloaded in two versions. One of the versions uses the following syntax:

public virtual XmlAttribute SetAttributeNode(XmlAttribute newAttr);

This method expects an XmlAttribute object. Here is an example that looks for a particular video in a collection and adds an ISBN attribute to it:

using System.IO;
using System.Xml;

public class Exercise
{
    public static int Main(string[] args)
    {
        XmlDocument xdVideos = new XmlDocument();

        if (File.Exists(@"C:\Videos Collection\Videos.xml"))
        {
            // Open the XML file
            xdVideos.Load(@"C:\Videos Collection\Videos.xml");

            // Create a new attribute
            System.Xml.XmlAttribute xaVideo = xdVideos.CreateAttribute("ISBN");
            xaVideo.Value = "0-7907-3900-3";

            // Get a list of elements whose names are Video
            XmlNodeList xnlVideos = xdVideos.GetElementsByTagName("video");
            // Since we will look for a specific video, get the list of all titles
            XmlNodeList xnlTitles = xdVideos.GetElementsByTagName("title");

            // Visit each title
            for (int i = 0; i < xnlTitles.Count; i++)
            {
                // Look for a video whose title is "Her Alibi"
                if (xnlTitles[i].InnerText.Equals("Her Alibi"))
                {
                    // Once you find that video, add the new attribute to it
                    ((XmlElement)(xnlVideos[i])).SetAttributeNode(xaVideo);
                }
            }

            xdVideos.Save(@"C:\Videos Collection\Videos.xml");
        }

        return 333_333;
    }
}

From the above Videos.xml file, this code would result in:

<?xml version="1.0" encoding="utf-8"?>
<videos FileDesc="Personal Video Collection">
  <video ISBN="0-7888-1623-3">
    <title Screenplay="Marty Kaplan">The Distinguished Gentleman</title>
    <director>Jonathan Lynn</director>
    <actors>
    </actors>
    <length>112 Minutes</length>
    <format>DVD</format>
    <rating>R</rating>
  </video>
  <video ISBN="0-7907-3900-3">
    <title WrittenBy="Charlie Peter">Her Alibi</title>
    <director>Bruce Beresford</director>
    <length>94 Mins</length>
    <format>DVD</format>
    <rating>PG-13</rating>
  </video>
</videos>

The Parent of an Attribute

Once an attribute has been created, to let you identify the element to which it belongs, the XmlAttribute class is equipped with a property named OwnerElement. This property produces an XmlElement value.

Attribute Removal

If an element has an attribute you don't want or that you don't need anymore, you can delete that attribute. You have various options, two are available through the XmlElement class.

The attributes of an XmlElement object are considered stored in an indexed list with the most left attribute at index 0, the second from left at index 1, and so on. Based on this, to remove an attribute by locating it based on its index, you can call the XmlElement.RemoveAttributeAt() method. Its syntax is:

public virtual XmlNode RemoveAttributeAt(int i);

When calling this method, if an attribute exists at position i, it will be deleted and the method would return it. If there is no attribute at that index, the method doesn't do anything and it returns 0.

Using the XmlElement.RemoveAttributeAt() method to delete an attribute can be uncertain because you would not know whether there is an attribute at the specified position. An alternative is to specify the name of the attribute you want to delete. To support this, the XmlElement class is equipped with the RemoveAttribute() method, which is overloaded with two versions. One of the versions of this method uses the following syntax:

public virtual void RemoveAttribute(string name);

This method expects as argument the name of the attribute to remove.

Another technique you can use consists of defining an XmlAttribute object and submitting to its XmlElement parent to delete. To do this, you can call the RemoveAttributeNode() method of the XmlElement object. Its syntax is:

public virtual XmlAttribute RemoveAttributeNode(XmlAttribute oldAttr);

When calling this method, pass the attribute object as argument. If the attribute exists, it would be removed and the method would return the deleted attribute. If the attribute doesn't exist, nothing would happen.

The Collection of Attributes of an Element

Introduction

So far, we have used only one attribute per element. Fortunately, you can create as many attributes as you judge necessary in an element. To do this, type the name of each attribute, assign it a double-quoted string and separate the attribute from the next with an empty space. Here is an example of an element with different attributes:

<video ISBN="0-7888-1623-3" ScreenRatio="Standard" SoundtrackAvailable="True" />

As mentioned already and as you should always remember, attributes belong to an element. To support them, the attributes of an element are stored in the Attributes property of the XmlElement class. The XmlElement.Attributes property is based on a class called XmlAttributeCollection.

To know the number of attributes in an element, you can use the XmlNamedNodeMap.Count property.

Attribute Addition

Whether using its index or name, after accessing an attribute, you can manipulate it as you see fit. For example, you can change or delete it using the same techniques we saw to perform on an individual attribute.

As mentioned already, the attributes are stored as a list. Because you have complete access to this list and the positions of its attributes, when creating or adding a new attribute, you can specify the position the new attribute should have in the collection. To create an attribute as the first in an element, you can call the XmlAttributeCollection.Prepend() method. Its syntax is:

public virtual XmlAttribute Prepend(XmlAttribute node);

Another technique you can use consists of locating an attribute first. Once you have one, to create a new attribute before it, you can call the XmlAttributeCollection.InsertBefore() method. Its syntax is:

public virtual XmlAttribute InsertBefore(XmlAttribute newNode,
					                     XmlAttribute refNode);

To add a new attribute after the current one, you can call the XmlAttributeCollection.InsertAfter() method. Its syntax is:

public virtual XmlAttribute InsertAfter(XmlAttribute newNode,
					                    XmlAttribute refNode);

To add an attribute at the end of the list of attributes of an element, you can call the XmlAttributeCollection.Append() method. Its syntax is:

public virtual XmlAttribute Append(XmlAttribute node); 

Access to an XML Attribute

To access an attribute by its position in the collection, you can use the XmlNamedNodeMap.Item() method.

The XmlAttributeCollection class is equipped with an ItemOf indexed property. This property is overloaded in three versions. The first version has the following syntax:

public virtual XmlAttribute this[int i] {get;}

This property allows you to access an attribute by considering that the attributes are stored in an array. The first or most left attribute has an index of 0; the second attribute from left (of course without counting the name of the element) has an index of 1, and so on.

It can be difficult and sometimes unpredictable, in some scenarios, to access an attribute by its index because you must know exactly where each attribute is positioned. Consider the following version of our Videos.xml XML file:

<?xml version="1.0" encoding="utf-8" ?>
<videos FileDesc="Personal Video Collection">
    <video ISBN="0-7888-1623-3"
	   ScreenRatio="Standard"
	   SoundtrackAvailable="True">
        <title StoryBy="Marty Kaplan and Jonathan Reynold"
	       Screenplay="Marty Kaplan">The Distinguished Gentleman</title>
        <director>Jonathan Lynn</director>
        <actors></actors>
        <length>112 Minutes</length>
        <format>DVD</format>
        <rating>R</rating>
    </video>
    <video ISBN="0-7907-3900-3">
        <title Screenplay="Charlie Peter">Her Alibi</title>
        <director>Bruce Beresford</director>
        <length>94 Mins</length>
        <format>DVD</format>
        <rating>PG-13</rating>
    </video>
</videos>

In the first video, the name of the screenplay writer is stored at index 1. In the second video, the name of the screenplay writer is stored at index 0. In this case, it may not be a good item to use the index to locate an attribute. Fortunately, the second version of the overloaded XmlAttributeCollection.ItemOf[] property has the following syntax:

public virtual XmlAttribute this[string name] {get;}

With this version, you can explicitly specify the name of the attribute that you want.

Attribute Removal

Using the list of attributes of an element, you can delete one or all attributes of an element. Since the attributes are stored in a collection, you can locate the undesired attribute by its index and then delete it. To do this, you can call the XmlAttributeCollection.RemoveAt() method. Its syntax is:

public virtual XmlAttribute RemoveAt(int i);

This method expects the index of the attribute that needs to be removed. As mentioned for the XmlAttributeCollection.ItemOf indexed property, to efficiently use this RemoveAt() method, you should know the exact index of the attribute, otherwise, you may access and therefore delete the wrong attribute. An alternative is to explicitly identify the attribute you want to delete. To do this, you can call the XmlAttributeCollection.Remove() method. Its syntax is:

public virtual XmlAttribute Remove(XmlAttribute node);

This method takes as attribute the XmlAttribute identification of the attribute you want to remove.

To delete all attributes of an element, you can call the XmlAttributeCollection.RemoveAll() method. Its syntax is:

public virtual void RemoveAll();

This method would simply remove all attributes that belong to an XmlElement object.

Comments in an XML Document

Introduction

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

A comment is a character, a line of text, 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.

Manually Adding a Comment to an XML Document

To create a comment, 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"?>
<!-- In this collection, we will keep each title "as is" -->
<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>

Progranmatically Adding a Comment

To support comments in an XML document, the .NET Framework provides a class named XmlComment. That class is defined in the System.Xml namespace.

To let you get the name of a comment, the XmlComment class is equipped with a property named Name.

To let you create a comment, the XmlDocument class is equipped with a method named CreateComment. Its syntax is:

public virtual XmlComment CreateComment(string data);

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

Introduction

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 document, 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.

Manually Creating a CDATA Section

To get a CDATA section in an XML document, 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"?>
<!-- In this collection, we will keep each title "as is" -->
<videos>
  <![CDATA[<VdoColl>Collection of Videos</VdoColl>]]>
  <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>

Programmatically Creating a CDATA Section

To support CDATA section, the .NET Framework provides a class named XmlCDataSection. This class is equipped with a Name property that allows you to get 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 virtual XmlCDataSection CreateCDataSection(string data);

Previous Copyright © 2005-2021, FunctionX Next