XPath and Arrays

Introduction

Consider the following XML document whose file is named Videos.xml:

<?xml version="1.0" encoding="utf-8"?>
<videos>
  <video>
    <title>Her Alibi</title>
    <director>Bruce Beresford</director>
    <length>94</length>
    <format>DVD</format>
    <rating>PG-13</rating>
  </video>
  <video>
    <title>The War of the Roses</title>
    <director>Danny DeVito</director>
    <cast-members>
      <actor>Michael Douglas</actor>
      <actor>Kathleen Turner</actor>
      <actor>Danny DeVito</actor>
    </cast-members>
  </video>
  <video>
    <title>The Distinguished Gentleman</title>
    <director>Jonathan Lynn</director>
    <cast-members>
      <actor>Eddie Murphy</actor>
      <actor>Lane Smith</actor>
      <actor>Sheryl Lee Ralph</actor>
      <actor>Joe Don Baker</actor>
      <actor>Victoria Rowell</actor>
    </cast-members>
    <cast-members>
      <actor>Charles S. Dutton</actor>
      <actor>Grant Shaud</actor>
      <actor>Kevin McCarthy</actor>
      <actor>Victor Rivers</actor>
      <actor>Chi McBride</actor>
      <actor>Noble Willingham</actor>
    </cast-members>
    <length>112</length>
    <format>DVD</format>
    <rating>R</rating>
    <year-released>1992</year-released>
    <categories>
      <genre>Comedy</genre>
      <genre>Politics</genre>
      <keywords>
        <keyword>satire</keyword>
        <keyword>government</keyword>
        <keyword>con artist</keyword>
        <keyword>lobbyist</keyword>
        <keyword>election</keyword>
      </keywords>
    </categories>
  </video>
  <video>
    <title>Duplex</title>
    <director>Danny DeVito</director>
    <cast-members>
      <narrator>Danny DeVito</narrator>
    </cast-members>
  </video>
  <video>
    <title>The Day After Tomorrow</title>
    <director>Roland Emmerich</director>
    <length>124</length>
    <categories>
      <genre>Drama</genre>
      <genre>Environment</genre>
      <genre>Science Fiction</genre>
    </categories>
    <format>BD</format>
    <rating>PG-13</rating>
    <keywords>
      <keyword>climate</keyword>
      <keyword>global warming</keyword>
      <keyword>disaster</keyword>
      <keyword>new york</keyword>
    </keywords>
  </video>
  <video>
    <title>Other People&#039;s Money</title>
    <director>Alan Brunstein</director>
    <year-released>1991</year-released>
    <cast-members>
      <actor>Danny DeVito</actor>
      <actor>Gregory Peck</actor>
      <actor>Penelope Ann Miller</actor>
    </cast-members>
    <cast-members>
      <actor>Dean Jones</actor>
      <actor>Piper Laurie</actor>
    </cast-members>
    <categories>
      <genre>Comedy</genre>
      <keywords>
        <keyword>satire</keyword>
        <keyword>female stocking</keyword>
        <keyword>seduction</keyword>
      </keywords>
      <genre>Business</genre>
      <keywords>
        <keyword>capitalism</keyword>
        <keyword>corporate take-over</keyword>
        <keyword>factory</keyword>
        <keyword>speech</keyword>
        <keyword>public speaking</keyword>
      </keywords>
      <genre>Drama</genre>
      <keywords>
        <keyword>play</keyword>
        <keyword>hostile take-over</keyword>
        <keyword>corporate raider</keyword>
      </keywords>
    </categories>
  </video>
</videos>

If you pass an expression that accesses many nodes on the same level and the expression produces many results, as you know already, the results are stored in an XmlNodeList collection. Based on the XPath standards, those results are stored in an array.

Accessing a Node by its Position

When accessing the results of an XPath expression, each result uses a specific position in the resulting array. The position corresponds to the index of arrays seen in C#, except that the indexes start at 1 (in most C-based arrays, which includes C#, the indexes of arrays start at 0). The first result is positioned at index 1, the second at index 2, until the end.

To access an individual result of the XPath expression, use the square brackets as done in C-based languages. Here is an example:

using System;
using System.Xml;
using System.Windows.Forms;

public class Exercise : Form
{
    public static int Main()
    {
        XmlDocument xdVideos = new XmlDocument();

	xdVideos.Load("../../Videos.xml");

	XmlElement xeVideo = xdVideos.DocumentElement;
	XmlNodeList xnlVideos = xeVideo.SelectNodes("/videos/video[1]");

	foreach(XmlNode xnVideo in xnlVideos)
	    MessageBox.Show(string.Format("First Video\n{0}", 
			    xnVideo.OuterXml),
			    "Video Collection",
			    MessageBoxButtons.OK,
			    MessageBoxIcon.Information);

	return 0;
    }
}

This would produce:

Accessing a Node by its Position

The array technique can be very valuable if the results are made of single values, such as the names of people. Consider the following example:

using System;
using System.Xml;
using System.Drawing;
using System.Windows.Forms;

public class Exercise : Form
{
    ListBox lbxAllActors, lbxLeadingActors;

    public Exercise()
    {
	lbxAllActors = new ListBox();
	lbxAllActors.Height = 220;
	lbxAllActors.Location = new Point(12, 12);
	Controls.Add(lbxAllActors);

	lbxLeadingActors = new ListBox();
	lbxLeadingActors.Height = 220;
	lbxLeadingActors.Location = new Point(140, 12);
	Controls.Add(lbxLeadingActors);

	Text = "Video Collection";

        XmlDocument xdVideos = new XmlDocument();

	xdVideos.Load("../../Videos.xml");

	XmlElement xeVideo = xdVideos.DocumentElement;
	XmlNodeList xnlVideos = xeVideo.SelectNodes("//videos/video/cast-members/actor");  

	foreach(XmlNode xnVideo in xnlVideos)
	    lbxAllActors.Items.Add(xnVideo.InnerText);

	xnlVideos = xeVideo.SelectNodes("//videos/video/cast-members/actor[1]");  

	foreach(XmlNode xnVideo in xnlVideos)
	    lbxLeadingActors.Items.Add(xnVideo.InnerText);
    }


    public static int Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
	Application.Run(new Exercise());

	return 0;
    }
}

This would produce the first actor in each section (each child of the root):

Accessing a Node by its Position

If you pass an index that is not found, such as one that is higher than the number of results, the method would return nothing (no exception would be thrown).

Once you have accessed a node by its position, if that node has at least one child node, you can access that child node using / and its name. Here is an example:

using System;
using System.Xml;
using System.Windows.Forms;

public class Exercise
{
    public static int Main()
    {
        XmlDocument xdVideos = new XmlDocument();

	xdVideos.Load("../../Videos.xml");

	XmlElement xeVideo = xdVideos.DocumentElement;
	XmlNodeList xnlVideos = xeVideo.SelectNodes("/videos/video[2]/cast-members");

	foreach(XmlNode xnVideo in xnlVideos)
	    MessageBox.Show(xnVideo.OuterXml,
			    "Videos with both Format and Category sections",
			    MessageBoxButtons.OK,
			    MessageBoxIcon.Information);

	return 0;
    }
}

This would produce:

Accessing a Node by its Position

Accessing a Node by its Position

Accessing the Lineage by Position

If the element on which you apply the square brackets has child nodes, you can apply the square brackets on the child nodes to get one at a position of your choice. Here is an example:

using System;
using System.Xml;
using System.Windows.Forms;

public class Exercise
{
    public static int Main()
    {
        XmlDocument xdVideos = new XmlDocument();

	xdVideos.Load("../../Videos.xml");

	XmlElement xeVideo = xdVideos.DocumentElement;
	XmlNodeList xnlVideos = xeVideo.SelectNodes("/videos[1]/video[3]");    

	foreach(XmlNode xnVideo in xnlVideos)
	    MessageBox.Show("Video\n" + xnVideo.OuterXml),
			    "Video Collection",
			    MessageBoxButtons.OK,
			    MessageBoxIcon.Information);

	return 0;
    }
}

This would produce:

Accessing the Lineage by Position

In the same way, you can apply the square brackets to any child node of an element. Here is an example:

using System;
using System.Xml;
using System.Windows.Forms;

public class Exercise
{
    public static int Main()
    {
        XmlDocument xdVideos = new XmlDocument();

	xdVideos.Load("../../Videos.xml");

	XmlElement xeVideo = xdVideos.DocumentElement;
	XmlNodeList xnlVideos = xeVideo.SelectNodes("/videos[1]/video[4]/categories[1]/genre[3]");    

	foreach(XmlNode xnVideo in xnlVideos)
	    MessageBox.Show(xnVideo.InnerXml,
			    "Video Collection",
			    MessageBoxButtons.OK,
			    MessageBoxIcon.Information);

	return 0;
    }
}

This would produce:

Accessing the Lineage by Position

The rules to remember are:

Using these features of arrays applied to XPath, you can access any node. For example, you can apply the square brackets to a certain node and access it by its position. If the node at that position has child nodes, you can use the name of those child nodes and apply the square brackets on that name to indicate what node you want to access. Consider the following example:

sing System;
using System.Xml;
using System.Drawing;
using System.Windows.Forms;

public class Exercise : Form
{
    Label   lblActor;
    TextBox txtActor;

    public Exercise()
    {
	lblActor = new Label();
	lblActor.Text = "Actor:";
	lblActor.AutoSize = true;
	lblActor.Location = new Point(12, 14);
	Controls.Add(lblActor);

	txtActor = new TextBox();
	txtActor.Location = new Point(60, 12);
	Controls.Add(txtActor);

	Text = "Video Collection";

        XmlDocument xdVideos = new XmlDocument();

	xdVideos.Load("../../Videos.xml");

	XmlElement xeVideo = xdVideos.DocumentElement;
	// This expression first gets the 2nd video.
	// Then it gets the 3rd actor of the cast-members section
	XmlNodeList xnlVideos = xeVideo.SelectNodes("/videos/video[2]/cast-members/actor[3]");    

	foreach(XmlNode xnVideo in xnlVideos)
	    txtActor.Text = xnVideo.InnerText;
    }


    public static int Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
	Application.Run(new Exercise());

	return 0;
    }
}

This would produce:

Accessing a Node by its Position

The First Child Node of the Root

As mentioned already, all the child nodes of an element are stored in an array. In fact, if you pass the name of the root as your XPath expression, this is equivalent to applying [1] to it. Here is an example:

using System;
using System.Xml; 
using System.Windows.Forms;

public class Exercise
{
    public static int Main()
    {
        XmlDocument xdVideos = new XmlDocument();

	   xdVideos.Load("../../Videos.xml");

	   XmlElement xeVideo = xdVideos.DocumentElement;
	    XmlNodeList xnlVideos = xeVideo.SelectNodes("/videos[1]");

	    foreach(XmlNode xnVideo in xnlVideos)
		MessageBox.Show(xnVideo.InnerXml,
			        "Video Collection",
			        MessageBoxButtons.OK,
			        MessageBoxIcon.Information);

	return 0;
    }
}

Accessing a Node by its Name

By using arrays, the XPath language makes it possible to get a collection of nodes based on their common name. To get only the nodes that have a specific section, pass the name of that section to the square brackets of the parent element. From our XML document, imagine you want only the videos that have a section named cast-members. Here is an example of getting them:

using System;
using System.Xml;
using System.Windows.Forms;

public class Exercise
{
    public static int Main()
    {
        XmlDocument xdVideos = new XmlDocument();

	xdVideos.Load("../../Videos.xml");

	XmlElement xeVideo = xdVideos.DocumentElement;
	XmlNodeList xnlVideos = xeVideo.SelectNodes("/videos/video[cast-members]");    

	foreach(XmlNode xnVideo in xnlVideos)
	    MessageBox.Show(string.Format("Video --------------------------------------------------------------------\n{0}",
                            xnVideo.OuterXml),
			    "Videos that have a cast-members section",
			    MessageBoxButtons.OK,
			    MessageBoxIcon.Information);

	return 0;
    }
}

This would produce:

Accessing a Node by its Name

Accessing a Node by its Name

Accessing a Node by its Name

Notice that, in our XML document, only some of the videos include the list of actors. If you pass a name that oesn't exist, the interpreter would produce an empty result (no exception would be thrown).

Locating a Specific Node by its Name

Considering the following XPath expression:

using System;
using System.Xml;
using System.Windows.Forms;

public class Exercise
{
    public static int Main()
    {
        XmlDocument xdVideos = new XmlDocument();

	xdVideos.Load("../../Videos.xml");

	XmlElement xeVideo = xdVideos.DocumentElement;
	XmlNodeList xnlVideos = xeVideo.SelectNodes("/videos/video/cast-members[actor]");    

	foreach(XmlNode xnVideo in xnlVideos)
	    MessageBox.Show(xnVideo.OuterXml,
			    "Videos with both Format and Category sections",
			    MessageBoxButtons.OK,
			    MessageBoxIcon.Information);

	return 0;
    }
}

This would produce all cast-members sections of all videos (only the cast-members section). If you want to get a only a specific child node, assign its value to the name in the square brackets. Here is an example:

using System;
using System.Xml;
using System.Windows.Forms;

public class Exercise
{
    public static int Main()
    {
        XmlDocument xdVideos = new XmlDocument();

	xdVideos.Load("../../Videos.xml");

	XmlElement xeVideo = xdVideos.DocumentElement;
	XmlNodeList xnlVideos = xeVideo.SelectNodes("/videos/video/cast-members[actor = 'Penelope Ann Miller']");    

	foreach(XmlNode xnVideo in xnlVideos)
	    MessageBox.Show(xnVideo.OuterXml,
			    "Videos with both Format and Category sections",
			    MessageBoxButtons.OK,
			    MessageBoxIcon.Information);

	return 0;
    }
}

Based on our XML document, the result would produce only the cast-members sections that include Penelope Ann Miller.

Accessing Grand-Children of a Node by its Name

After accessing a node by name, if it has at least one child node, you can access it/them by passing its/their name after the parent and /. Here is an example:

using System;
using System.Xml;
using System.Windows.Forms;

public class Exercise
{
    public static int Main()
    {
        XmlDocument xdVideos = new XmlDocument();

	xdVideos.Load("../../Videos.xml");

	XmlElement xeVideo = xdVideos.DocumentElement;
	XmlNodeList xnlVideos = xeVideo.SelectNodes("/videos/video[categories]/keywords");

	foreach(XmlNode xnVideo in xnlVideos)
	    MessageBox.Show(xnVideo.OuterXml,
			    "Videos with both Format and Category sections",
			    MessageBoxButtons.OK,
			    MessageBoxIcon.Information);

	return 0;
    }
}

Accessing a Node by its Value

If you want to locate a specific child node, in the square brackets, type .= and the value of the node. Here is an example:

using System;
using System.Xml;
using System.Windows.Forms;

public class Exercise
{
    public static int Main()
    {
        XmlDocument xdVideos = new XmlDocument();

	xdVideos.Load("../../Videos.xml");

	XmlElement xeVideo = xdVideos.DocumentElement;
	XmlNodeList xnlVideos = xeVideo.SelectNodes("/videos/video/cast-members/actor[. = 'Eddie Murphy']");    

	foreach(XmlNode xnVideo in xnlVideos)
	    MessageBox.Show(xnVideo.InnerText,
			    "Videos with both Format and Category sections",
			    MessageBoxButtons.OK,
			    MessageBoxIcon.Information);

	return 0;
    }
}

Accessing the Nodes With Specific Grand-Children

To get only the parent nodes that have child nodes that in turn have certain child nodes, in the square brackets, type the names of the child node and grand-child separated by /. Here an example:

using System;
using System.Xml;
using System.Windows.Forms;

public class Exercise
{
    public static int Main()
    {
        XmlDocument xdVideos = new XmlDocument();

	xdVideos.Load("../../Videos.xml");

	XmlElement xeVideo = xdVideos.DocumentElement;
	XmlNodeList xnlVideos = xeVideo.SelectNodes("/videos/video[categories/keywords]");    

	foreach(XmlNode xnVideo in xnlVideos)
	    MessageBox.Show(xnVideo.OuterXml,
			    "Videos with both Format and Category sections",
			    MessageBoxButtons.OK,
			    MessageBoxIcon.Information);

	return 0;
    }
}

From our XML file, this would produce only the videos that have a categories section but that categories section must have a keywords section as child, which excludes a video where the categories and their keywords are children on the same level:

Accessing the Nodes With Specific Grand-Children

Accessing the Nodes With Specific Grand-Children

Accessing the Grand-Children by Position

Notice that our XML document has some videos that have a cast-members section. Instead of getting all of them, you can ask the interpreter to return only the one at a specific position. This is done by adding a second pair of square brackets and passing the desired index. Here is an example:

using System;
using System.Xml;
using System.Windows.Forms;

public class Exercise
{
    public static int Main()
    {
        XmlDocument xdVideos = new XmlDocument();

	xdVideos.Load("../../Videos.xml");

	XmlElement xeVideo = xdVideos.DocumentElement;
	XmlNodeList xnlVideos = xeVideo.SelectNodes("/videos/video[cast-members][2]");    

	foreach(XmlNode xnVideo in xnlVideos)
	    MessageBox.Show(xnVideo.OuterXml,
			    "Second video with a cast-members section",
			    MessageBoxButtons.OK,
			    MessageBoxIcon.Information);

	return 0;
    }
}

This would produce:

Accessing the Grand-Children by Position

Accessing the Grand-Children by Name

After getting a child node by name, if that child node has at least one child node and you know the name of that grand-child, pass that name to the square brackets applied after /. Here is an example:

using System;
using System.Xml;
using System.Windows.Forms;

public class Exercise
{
    public static int Main()
    {
        XmlDocument xdVideos = new XmlDocument();

	xdVideos.Load("../../Videos.xml");

	XmlElement xeVideo = xdVideos.DocumentElement;
	XmlNodeList xnlVideos = xeVideo.SelectNodes("/videos/video[categories]/keywords[keyword]");

	foreach(XmlNode xnVideo in xnlVideos)
	    MessageBox.Show(xnVideo.OuterXml,
			    "Videos with both Format and Category sections",
			    MessageBoxButtons.OK,
			    MessageBoxIcon.Information);

	return 0;
    }
}

This would produce:

Accessing the Grand-Children by Position

XPath Functions

Introduction

The XPath language provides many functions. Some functions are made to perform some of the operations we have applied already. Some other functions are meant to produce some results we have not gotten so far.

The Position of a Node

We have seen that we can use the square brackets to get to the position of a node. The XPath language has a function named position that can be used to access a node based on its position. To use it, in the square brackets of the node that is considered the parent, assign the desired position to position().

As you may know already, the first child of a node has an index of 1. Therefore, to get the first child node, assign 1 to the position() function. Here is an example:

using System;
using System.Xml;
using System.Windows.Forms;

public class Exercise
{
    public static int Main()
    {
        XmlDocument xdVideos = new XmlDocument();

	xdVideos.Load("../../Videos.xml");

	XmlElement xeVideo = xdVideos.DocumentElement;
	XmlNodeList xnlVideos = xeVideo.SelectNodes("/videos/video[position() = 1]");    

	foreach(XmlNode xnVideo in xnlVideos)
	    MessageBox.Show(xnVideo.OuterXml,
			    "Video Collection: First Video",
			    MessageBoxButtons.OK,
			    MessageBoxIcon.Information);

	return 0;
    }
}

This would produce:

The Position of a Node

In the same way, to get to any node, assign its index to position().

The Last Child Node

To help you get to the last child node of a node, XPath proposes a function named last. Therefore, to get the last child node, pass last() as the index of the node that acts as the parent. Here is an example:

using System;
using System.Xml;
using System.Windows.Forms;

public class Exercise
{
    public static int Main()
    {
        XmlDocument xdVideos = new XmlDocument();

	xdVideos.Load("../../Videos.xml");

	XmlElement xeVideo = xdVideos.DocumentElement;
	// This expression first gets the 2nd video.
	// Then it gets the 3rd actor of that video
	XmlNodeList xnlVideos = xeVideo.SelectNodes("/videos/video[last()]");    

	foreach(XmlNode xnVideo in xnlVideos)
	    MessageBox.Show(string.Format("Video: {0}", 
                                          xnVideo.OuterXml),
			    "Video Collection",
			    MessageBoxButtons.OK,
			    MessageBoxIcon.Information);

	return 0;
    }
}

This would produce:

The Last Child Node

As an alternative, you can assign last() to position(). Here is an example:

using System;
using System.Xml;
using System.Windows.Forms;

public class Exercise
{
    public static int Main()
    {
        XmlDocument xdVideos = new XmlDocument();

	xdVideos.Load("../../Videos.xml");

	XmlElement xeVideo = xdVideos.DocumentElement;

	XmlNodeList xnlVideos = xeVideo.SelectNodes("/videos/video[position() = last()]");    

	foreach(XmlNode xnVideo in xnlVideos)
	    MessageBox.Show(string.Format("Video: {0}", 
                                          xnVideo.OuterXml),
					  "Video Collection",
					  MessageBoxButtons.OK,
					  MessageBoxIcon.Information);

	return 0;
    }
}

Consider the following example:

using System;
using System.Xml;
using System.Windows.Forms;

public class Exercise
{
    public static int Main()
    {
        XmlDocument xdVideos = new XmlDocument();

	xdVideos.Load("../../Videos.xml");
	XmlElement xeVideo = xdVideos.DocumentElement;
	XmlNodeList xnlVideos = xeVideo.SelectNodes("/videos/video/cast-members[last()]");    

	foreach(XmlNode xnVideo in xnlVideos)
	    MessageBox.Show(string.Format("Last cast-members section of any video " +
			    "that has a cast-members section\n" +
			    "____________________________________________________\n{0}",
 			    xnVideo.OuterXml),
			    "Video Collection",
			    MessageBoxButtons.OK,
			    MessageBoxIcon.Information);

	return 0;
    }
}

This would produce:

The Last Child Node

The Last Child Node

The Last Child Node

Notice that the result includes all nodes from a parent that has a cast-members section. If you want to get only the last node that has that section, include the whole path in parentheses excluding the square brackets and their index. Here is an example:

using System;
using System.Xml;
using System.Windows.Forms;

public class Exercise
{
    public static int Main()
    {
        XmlDocument xdVideos = new XmlDocument();

	xdVideos.Load("../../Videos.xml");
	XmlElement xeVideo = xdVideos.DocumentElement;
	XmlNodeList xnlVideos = xeVideo.SelectNodes("(/videos/video/cast-members)[last()]");    

	foreach(XmlNode xnVideo in xnlVideos)
	    MessageBox.Show(string.Format("Last cast-members section\n" +
			    "____________________________________________________\n{0}",
                            xnVideo.InnerXml),
			    "Video Collection",
			    MessageBoxButtons.OK,
			    MessageBoxIcon.Information);

	return 0;
    }
}

This would produce:

The Last Child Node

XPath and Boolean Operations: Boolean Node Searching

AND Both Child Nodes

You may have noticed that, in our XML document, some parent nodes include some child nodes that are not found in other parent nodes. For example, the first, the second, and the third videos of our XML document have a <format> child node but the fourth video does not. On the other hand, the second, the third, and the fourth videos have a <categories> child node while the first does not.

You can ask the XmlNodeList.SelectNodes() method (in fact the XML interpreter) to produce in its results only the parent nodes that have a combination of certain two nodes. To get such a result, add a first combination that includes one of the names of child nodes. Include a second pair of square brackets with the other name. Here is an example:

using System;
using System.Xml;
using System.Windows.Forms;

public class Exercise
{
    public static int Main()
    {
        XmlDocument xdVideos = new XmlDocument();

	xdVideos.Load("../../Videos.xml");

	XmlElement xeVideo = xdVideos.DocumentElement;
	XmlNodeList xnlVideos = xeVideo.SelectNodes("/videos/video[format][categories]");    

	foreach(XmlNode xnVideo in xnlVideos)
	    MessageBox.Show(xnVideo.OuterXml,
			    "Videos with both Format and Category sections",
			    MessageBoxButtons.OK,
			    MessageBoxIcon.Information);

	return 0;
    }
}

This would produce:

AND Both Child Nodes

AND Both Child Nodes

The "and" operator is used to check that two child nodes are found under a parent node. To use it, create the square brackets and in them, provide the names of two child nodes separated by the and operator. Here is an example:

XmlNodeList xnlVideos = xeVideo.SelectNodes("//videos/video[format and rating]");

In this case, only parent nodes that have both child nodes would be produced.

OR Either Child Node

To produce the parent nodes that include one or the other child node, use the or operator. Here is an example:

using System;
using System.Xml;
using System.Windows.Forms;

public class Exercise
{
    public static int Main()
    {
        XmlDocument xdVideos = new XmlDocument();

	xdVideos.Load("../../Videos.xml");

	XmlElement xeVideo = xdVideos.DocumentElement;
	XmlNodeList xnlVideos = xeVideo.SelectNodes("/videos/video[cast-members or categories]");    

	foreach(XmlNode xnVideo in xnlVideos)
	    MessageBox.Show(xnVideo.OuterXml,
			    "Videos with both Format and Category sections",
			    MessageBoxButtons.OK,
			    MessageBoxIcon.Information);

	return 0;
    }
}

In this case, only parent nodes that have both child nodes would be produced:

AND Both Child Nodes

Accessing the Grand-Children by Position

AND Both Child Nodes

Accessing the Nodes With Specific Grand-Children

Once you have accessed the nodes, you can apply any concept we have reviewed so far to locate a specific node. Here is an example:

using System;
using System.Xml;
using System.Windows.Forms;

public class Exercise
{
    public static int Main()
    {
        XmlDocument xdVideos = new XmlDocument();

	xdVideos.Load("../../Videos.xml");

	XmlElement xeVideo = xdVideos.DocumentElement;
	XmlNodeList xnlVideos = xeVideo.SelectNodes("/videos/video/cast-members[actor or narrator][. = 'Danny DeVito']");    

	foreach(XmlNode xnVideo in xnlVideos)
	    MessageBox.Show(xnVideo.InnerText,
			    "Videos with both Format and Category sections",
			    MessageBoxButtons.OK,
			    MessageBoxIcon.Information);

	return 0;
    }
}

NOT This Child Node

To let you get only the nodes that do not have a certain child node, the XPath provides a Boolean function named not. To use it, pass the name of the node to it. Here is an example:

using System;
using System.Xml;
using System.Windows.Forms;

public class Exercise
{
    public static int Main()
    {
        XmlDocument xdVideos = new XmlDocument();

	xdVideos.Load("../../Videos.xml");

	XmlElement xeVideo = xdVideos.DocumentElement;
	XmlNodeList xnlVideos = xeVideo.SelectNodes("/videos/video[not(cast-members)]");

	foreach(XmlNode xnVideo in xnlVideos)
	    MessageBox.Show(xnVideo.OuterXml,
			    "Videos with both Format and Category sections",
			    MessageBoxButtons.OK,
			    MessageBoxIcon.Information);

	return 0;
    }
}

When the not() function returns, it produces only the nodes that don't have the child-node whose name was passed:

NOT This Child Node

NOT This Child Node

Sets Combinations

All the expressions we have seen so far produced only one result each. To let you combine two results into one, the XPath language provides the | operator. It must be applied between two expressions. Here is an example:

using System;
using System.Xml;
using System.Windows.Forms;

public class Exercise
{
    public static int Main()
    {
        XmlDocument xdVideos = new XmlDocument();

	xdVideos.Load("Videos.xml");

	XmlElement xeVideo = xdVideos.DocumentElement;
	XmlNodeList xnlVideos = xeVideo.SelectNodes("//cast-members[actor='Danny DeVito'] | //cast-members[actor='Eddie Murphy']");

	foreach(XmlNode xnVideo in xnlVideos)
	    MessageBox.Show(xnVideo.OuterXml,
			    "Videos with both Format and Category sections",
			    MessageBoxButtons.OK,
			    MessageBoxIcon.Information);

	return 0;
    }
}

Boolean Operations

Numeric Comparisons on a Node Value

The XPath language provides some operators that can be used to perform Booleans operations. The numeric comparisons are used to find the Boolean relationship between two values. Only some operators are allowed. If you use an operator that is not valid, the compiler would throw an exception. The valid Boolean operators and the rules are:

Here is an example:

using System;
using System.Xml;
using System.Windows.Forms;

public class Exercise
{
    public static int Main()
    {
        XmlDocument xdVideos = new XmlDocument();

	xdVideos.Load("../../Videos.xml");

	XmlElement xeVideo = xdVideos.DocumentElement;
	XmlNodeList xnlVideos = xeVideo.SelectNodes("//videos/video[length >= 100]");    

	foreach(XmlNode xnVideo in xnlVideos)
	    MessageBox.Show(xnVideo.InnerXml,
			    "Videos with both Format and Category sections",
			    MessageBoxButtons.OK,
			    MessageBoxIcon.Information);

	return 0;
    }
}

Logical Conjunction

Logical conjunction consists of combining two Boolean operations that must both produce true results. To create a logical conjunction, create some square brackets applied to the parent name of a node and create the conjunction operation in the square brackets. There are two main ways you can perform the operation:

In the same way, you can create as many conjunctions as you want, by separating the operations with the and operator.

Logical Disjunction

Logical disjunction consists of applying the same criterion to two nodes or applying different criteria to the same node so that at least one of the operations needs to be true. To create a logical disjunction, apply the square brackets to the parent node. As seen for the conjunction, there are two main ways to use a logical disjunction:

By using the Boolean search operators (and, or, and the not() functions) and the logical conjunction/disjunction operations, you can create tremendous combinations to include and exclude some nodes in the XPath expression and get the desired results.

XPath Axes

XPath Keywords

An axis is a technique of locating a node or a series of nodes using a path. To address this issue, the XPath language provides some keywords that can be used in the expressions. As we are going to see, some keywords are optional and some other keywords are necessary or useful. If you decide to include one of those keywords in your XPath expression, you must use only a valid keyword. If you use a keyword that is not recognized, the compiler will throw an XPathException exception.

When used, an axis keyword is followed by ::, followed by the name of a node or an operator.

Child Nodes

The child keyword is used to indicate that a child node must be accessed. The child keyword is followed by ::. If you want to see all child nodes, follow :: with *. Here is an example:

using System;
using System.Xml;
using System.Windows.Forms;

public class Exercise
{
    public static int Main()
    {
        XmlDocument xdVideos = new XmlDocument();

        xdVideos.Load("../../Videos.xml");

        XmlElement xeVideo = xdVideos.DocumentElement;
        XmlNodeList xnlVideos = xeVideo.SelectNodes("/videos/video/child::*");

        foreach (XmlNode xnVideo in xnlVideos)
            MessageBox.Show(xnVideo.InnerXml,
                            "Video Collection",
                            MessageBoxButtons.OK,
                            MessageBoxIcon.Information);

        return 0;
    }
}

That code would produce all nodes that are direct children of video. If a child node includes its own child nodes, the node and all its children would be considered as one. To get only specific child nodes, follow :: by the name of the child node to access. Here is an example that accesses the director child nodes:

using System;
using System.Xml;
using System.Windows.Forms;

public class Exercise
{
    public static int Main()
    {
        XmlDocument xdVideos = new XmlDocument();

        xdVideos.Load("../../Videos.xml");

        XmlElement xeVideo = xdVideos.DocumentElement;
        XmlNodeList xnlVideos = xeVideo.SelectNodes("/videos/video/child::director");

        foreach (XmlNode xnVideo in xnlVideos)
            MessageBox.Show(xnVideo.InnerXml,
                            "Video Collection",
                            MessageBoxButtons.OK,
                            MessageBoxIcon.Information);

        return 0;
    }
}

In the same way, you can access any child node by preceding it with the child keyword. In most cases, you can omit the child keyword. For example this:

XmlNodeList xnlVideos = xeVideo.SelectNodes(".//video/*/actor");

Is the same as:

using System;
using System.Xml;
using System.Drawing;
using System.Windows.Forms;

public class Exercise : Form
{
    ListBox lbxDirectors;

    public Exercise()
    {
        lbxDirectors = new ListBox();
        lbxDirectors.Location = new Point(12, 12);
        Controls.Add(lbxDirectors);

        Text = "Video Collection";

        XmlDocument xdVideos = new XmlDocument();

        xdVideos.Load("../../Videos.xml");

        XmlElement xeVideo = xdVideos.DocumentElement;
        XmlNodeList xnlVideos = xeVideo.SelectNodes(".//video/*/child::actor");

        foreach (XmlNode xnVideo in xnlVideos)
            lbxDirectors.Items.Add(xnVideo.InnerText);
    }


    public static int Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
	Application.Run(new Exercise());

        return 0;
    }
}

You can also apply any of the rules we have used so far. For example, the following code will produce the child nodes of the second video if the child node is named cast-members:

using System;
using System.Xml;
using System.Windows.Forms;

public class Exercise : Form
{
    public static int Main()
    {
        XmlDocument xdVideos = new XmlDocument();

        xdVideos.Load("../../Videos.xml");

        XmlElement xeVideo = xdVideos.DocumentElement;
        XmlNodeList xnlVideos = xeVideo.SelectNodes("/videos/video[2]/child::cast-members");

        foreach (XmlNode xnVideo in xnlVideos)
            MessageBox.Show(xnVideo.OuterXml,
                            "Video Collection",
                            MessageBoxButtons.OK,
                            MessageBoxIcon.Information);

        return 0;
    }
}

This would produce:

 

Accessing Specific Nodes

Accessing Specific Nodes

Parent Nodes

The parent keyword is used to get the parent of an element. Here is an example:

using System;
using System.Xml;
using System.Drawing;
using System.Windows.Forms;

public class Exercise : Form
{
    ListBox lbxDirectors;

    public Exercise()
    {
        lbxDirectors = new ListBox();
        lbxDirectors.Size = new System.Drawing.Size(100, 80);
        lbxDirectors.Location = new Point(12, 12);
        Controls.Add(lbxDirectors);

        Text = "Video Collection";

        XmlDocument xdVideos = new XmlDocument();

        xdVideos.Load("../../Videos.xml");

        XmlElement xeVideo = xdVideos.DocumentElement;
        XmlNodeList xnlVideos = xeVideo.SelectNodes("//parent::director");

        foreach (XmlNode xnVideo in xnlVideos)
            lbxDirectors.Items.Add(xnVideo.InnerText);
    }


    public static int Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Exercise());

        return 0;
    }
}

The Ancestors of a Node

The ancestors of a node are its parent and grand-parent, up to the root of the XML file. To get the ancestors of a node, precede its name with the ancestor keyword. Here is an example:

using System;
using System.Xml;
using System.Windows.Forms;

public class Exercise
{
    public static int Main()
    {
        XmlDocument xdVideos = new XmlDocument();

        xdVideos.Load("../../Videos.xml");

        XmlElement xeVideo = xdVideos.DocumentElement;
        XmlNodeList xnlVideos = xeVideo.SelectNodes("//ancestor::video");

        foreach (XmlNode xnVideo in xnlVideos)
            MessageBox.Show(xnVideo.InnerXml,
                            "Video Collection",
                            MessageBoxButtons.OK,
                            MessageBoxIcon.Information);

        return 0;
    }
}

The Descendants of a Node

The descendants of a node are its child(ren) and grand-child(ren), down to the last grand-child of that node. To get the descendants of a node, precede its name with the descendant keyword.

The Previous Sibling of a Node

The previous sibling of a node is a node that comes before it in the tree while both nodes are on the same level. To get the collection of nodes that come before a certain node, precede its name with the preceding keyword. Here is an example:

using System;
using System.Xml;
using System.Windows.Forms;

public class Exercise
{
    public static int Main()
    {
        XmlDocument xdVideos = new XmlDocument();

        xdVideos.Load("Videos.xml");

        XmlElement xeVideo = xdVideos.DocumentElement;
        XmlNodeList xnlVideos = xeVideo.SelectNodes("//preceding::categories");

        foreach (XmlNode xnVideo in xnlVideos)
            MessageBox.Show(xnVideo.OuterXml,
                            "Video Collection",
                            MessageBoxButtons.OK,
                            MessageBoxIcon.Information);

        return 0;
    }
}

If your expression indludes the preceding keyword, the result would include the node itself and the next node of the same name in the same tree level. If you want to exclude the node itself from the result, in other words if you want to get only the next sibliing of the same level, use the preceding-sibling keyword.

The Next Sibling a Node

The following keyword is used to get the next node(s) that come after the referenced one but of the same tree level. Consider the following expression:

using System;
using System.Xml;
using System.Windows.Forms;

public class Exercise : Form
{
    public static int Main()
    {
        XmlDocument xdVideos = new XmlDocument();

        xdVideos.Load("../../Videos.xml");

        XmlElement xeVideo = xdVideos.DocumentElement;
        XmlNodeList xnlVideos = xeVideo.SelectNodes("/videos/video/following::director");

        foreach (XmlNode xnVideo in xnlVideos)
            MessageBox.Show(xnVideo.OuterXml,
                            "Video Collection",
                            MessageBoxButtons.OK,
                            MessageBoxIcon.Information);

        return 0;
    }
}

This code would access the director nodes, excluding the first one.

XPath and XML Attributes

Introduction

As you should know already, an attribute is a type of element included in the start tag of a node. Here are examples of attributes in an XML document of a file named Videos.xml:

<?xml version="1.0" encoding="utf-8"?>
<videos common-name="Video Collection" purpose="To keep track of personal/home videos">
  <video shelf-number="CMD97904">
    <title>Her Alibi</title>
    <director>Bruce Beresford</director>
    <length>94</length>
    <format>DVD</format>
    <rating>PG-13</rating>
  </video>
  <video shelf-number="PLT24857">
    <title France="Monsieur le D&#233;put&#233;" Italy="Il Distinto Gentiluomo" Spain="Su Distinguida Señoría">The Distinguished Gentleman</title>
    <director>Jonathan Lynn</director>
    <cast-members>
      <actor role="Thomas Jefferson Johnson">Eddie Murphy</actor>
      <actor role="Dick Dodge">Lane Smith</actor>
      <actor role="Miss Loretta">Sheryl Lee Ralph</actor>
      <actor role="Olaf Andersen">Joe Don Baker</actor>
      <actor role="Celia Kirby">Victoria Rowell</actor>
    </cast-members>
    <cast-members>
      <actor role="Elijah Hawkins">Charles S. Dutton</actor>
      <actor role="Arthur Reinhardt">Grant Shaud</actor>
      <actor role="Terry Corrigan">Kevin McCarthy</actor>
      <actor role="Armando">Victor Rivers</actor>
      <actor role="Homer">Chi McBride</actor>
      <actor role="Zeke Bridges">Noble Willingham</actor>
    </cast-members>
    <length>112</length>
    <format screen="Wide Screen">DVD</format>
    <rating>R</rating>
    <year-released date-released="4 December 1992">1992</year-released>
    <categories>
      <genre>Comedy</genre>
      <genre>Politics</genre>
      <keywords>
        <keyword>satire</keyword>
        <keyword>government</keyword>
        <keyword>con artist</keyword>
        <keyword>lobbyist</keyword>
        <keyword>election</keyword>
      </keywords>
    </categories>
  </video>
  <video>
    <title>Duplex</title>
    <director>Danny DeVito</director>
    <cast-members>
      <narrator>Danny DeVito</narrator>
    </cast-members>
  </video>
  <video shelf-number="SCF13948">
    <title Spain="El Día de Mañana" Croatia="Dan Poslije Sutra" Portugal="O Dia Depois de Amanhã">The Day After Tomorrow</title>
    <director>Roland Emmerich</director>
    <length>124</length>
    <categories>
      <genre>Drama</genre>
      <genre>Environment</genre>
      <genre>Science Fiction</genre>
    </categories>
    <format>BD</format>
    <rating>PG-13</rating>
    <keywords>
      <keyword>climate</keyword>
      <keyword>global warming</keyword>
      <keyword>disaster</keyword>
      <keyword>new york</keyword>
    </keywords>
  </video>
  <video shelf-number="CMD93805">
    <title>Other People's Money</title>
    <director>Alan Brunstein</director>
    <year-released date-released="18 October 1991">1991</year-released>
    <cast-members>
      <actor role="Lawrence Garfield">Danny DeVito</actor>
      <actor role="Andrew Jorgenson">Gregory Peck</actor>
      <actor role="Kate Sullivan">Penelope Ann Miller</actor>
    </cast-members>
    <cast-members>
      <actor role="Bill Coles">Dean Jones</actor>
      <actor role="Bea Sullivan">Piper Laurie</actor>
    </cast-members>
    <categories>
      <genre>Comedy</genre>
      <keywords>
        <keyword>satire</keyword>
        <keyword>female stocking</keyword>
        <keyword>seduction</keyword>
      </keywords>
      <genre>Business</genre>
      <keywords>
        <keyword>capitalism</keyword>
        <keyword>corporate take-over</keyword>
        <keyword>factory</keyword>
        <keyword>speech</keyword>
        <keyword>public speaking</keyword>
      </keywords>
      <genre>Drama</genre>
      <keywords>
        <keyword>play</keyword>
        <keyword>hostile take-over</keyword>
        <keyword>corporate raider</keyword>
      </keywords>
    </categories>
  </video>
</videos>

To access an attribute, follow the name of its parent tag with a forward slash, an @ sign, and the name of the attribute. For example, if the root node has an attribute, use the formula:

/RootName/@AttributeName");

Here is an example:

using System;
using System.Xml; 
using System.Windows.Forms;

public class Exercise
{
    public static int Main()
    {
        XmlDocument xdVideos = new XmlDocument();

	xdVideos.Load("../../Videos.xml");

	XmlElement xeVideo = xdVideos.DocumentElement;
	XmlNodeList xnlVideos = xeVideo.SelectNodes("/videos/@common-name");

	foreach(XmlNode xnVideo in xnlVideos)
		MessageBox.Show(xnVideo.OuterXml,
			        "Video Collection",
			        MessageBoxButtons.OK,
			        MessageBoxIcon.Information);

	return 0;
    }
}

This would produce:

Introduction to XPath and XML Attributes

In the same way, use any of the formulas we have seen so far to access any node. Then, if the node has an attribute, use the @ sign to get that attribute. Here is an example:

using System;
using System.Xml; 
using System.Windows.Forms;

public class Exercise
{
    public static int Main()
    {
        XmlDocument xdVideos = new XmlDocument();

        xdVideos.Load("../../Videos.xml");

	XmlElement xeVideo = xdVideos.DocumentElement;
	XmlNodeList xnlVideos = xeVideo.SelectNodes("/videos/video/@shelf-number");

	foreach(XmlNode xnVideo in xnlVideos)
		MessageBox.Show(xnVideo.OuterXml,
				"Video Collection",
			        MessageBoxButtons.OK,
			        MessageBoxIcon.Information);

	return 0;
    }
}

This would produce:

 Introduction to XPath and XML Attributes    Introduction to XPath and XML Attributes

Introduction to XPath and XML Attributes    Introduction to XPath and XML Attributes

 

If you use an attribute that either doesn't exist or the element doesn't have that particular attribute, the result would be empty; no exception would be thrown.

Getting all Attributes of an Element

If you want to get all the attributes of an element, replace the name of the attribute with * as in @*. Here is an example:

using System;
using System.Xml;
using System.Drawing;
using System.Windows.Forms;

public class Exercise : Form
{
    Label   lblActorsRoles;
    ListBox lbxActorsRoles;

    public Exercise()
    {
	lblActorsRoles = new Label();
	lblActorsRoles.AutoSize = true;
	lblActorsRoles.Text = "Characters in Videos";
	lblActorsRoles.Location = new Point(12, 10);
	Controls.Add(lblActorsRoles);

	lbxActorsRoles = new ListBox();
	lbxActorsRoles.Size = new System.Drawing.Size(198, 90);
	lbxActorsRoles.Location = new Point(12, 28);
	Controls.Add(lbxActorsRoles);

	Text = "Video Collection";

        XmlDocument xdVideos = new XmlDocument();

	xdVideos.Load("../../Videos.xml");

	XmlElement xeVideo = xdVideos.DocumentElement;
	XmlNodeList xnlVideos = xeVideo.SelectNodes("/videos/video/title/@*");

	foreach(XmlNode xnVideo in xnlVideos)
	    lbxActorsRoles.Items.Add(xnVideo.OuterXml);

	Size = new System.Drawing.Size(230, 150);
    }


    public static int Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
	Application.Run(new Exercise());

	return 0;
    }
}

This would produce:

Getting all Attributes of an Element

Getting the Value of an Attribute

As we have seen so far, both the XPath language and the NET Framework provide various means to access an attribute. For example, the XPath language uses the @ sign to access an XML attribute while the XmlNode class is equipped with the OuterXml, the InnerXml, and the InnerText properties. Here is an example that uses the XmlNode.InnerXml property to access the attribute:

using System;
using System.Xml;
using System.Drawing;
using System.Windows.Forms;

public class Exercise : Form
{
    Label   lblActorsRoles;
    ListBox lbxActorsRoles;

    public Exercise()
    {
	lblActorsRoles = new Label();
	lblActorsRoles.AutoSize = true;
	lblActorsRoles.Text = "Characters in Videos";
	lblActorsRoles.Location = new Point(12, 10);
	Controls.Add(lblActorsRoles);

	lbxActorsRoles = new ListBox();
	lbxActorsRoles.Size = new System.Drawing.Size(250, 220);
	lbxActorsRoles.Location = new Point(12, 28);
	Controls.Add(lbxActorsRoles);

	Text = "Video Collection";

    	XmlDocument xdVideos = new XmlDocument();
	xdVideos.Load("../../Videos.xml");

	XmlElement xeVideo = xdVideos.DocumentElement;
	XmlNodeList xnlVideos = xeVideo.SelectNodes("/videos/video/cast-members/actor/@role");

	foreach(XmlNode xnVideo in xnlVideos)
	    lbxActorsRoles.Items.Add(xnVideo.InnerXml);
    }


    public static int Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
	Application.Run(new Exercise());

	return 0;
    }
}

This would produce:

Getting the Value of an Attribute

Remember that, to get all attributes, you can use @* as in:

XmlElement xeVideo = xdVideos.DocumentElement;
XmlNodeList xnlVideos = xeVideo.SelectNodes("/videos/video/cast-members/actor/@*");

foreach(XmlNode xnVideo in xnlVideos)
    lbxActorsRoles.Items.Add(xnVideo.InnerXml);

Notice that the XmlNode.InnerXml property applied to an attribute produces only the text of the attribute. In the same way, you can use the XmlNode.InnerText property to get the same result. On the other hand, the XmlNode.OuterXml property produces the name and value of an attribute. Here is an example:

XmlNodeList xnlVideos = xeVideo.SelectNodes("/videos/video/cast-members/actor/@role");

foreach(XmlNode xnVideo in xnlVideos)
    lbxActorsRoles.Items.Add(xnVideo.OuterXml);

This would produce:

Getting the Value of an Attribute

Getting an Attribute and its Parent

In some cases, you may want to get both the attribute and its parent. To do that, after the name of the element, include the @ and name of the attribute in square brackets. Here is an example:

using System;
using System.Xml;
using System.Drawing;
using System.Windows.Forms;

public class Exercise : Form
{
    Label   lblActorsRoles;
    ListBox lbxActorsRoles;

    public Exercise()
    {
	lblActorsRoles = new Label();
	lblActorsRoles.AutoSize = true;
	lblActorsRoles.Text = "Characters in Videos";
	lblActorsRoles.Location = new Point(12, 10);
	Controls.Add(lblActorsRoles);

	lbxActorsRoles = new ListBox();
	lbxActorsRoles.Size = new System.Drawing.Size(325, 220);
	lbxActorsRoles.Location = new Point(12, 28);
	Controls.Add(lbxActorsRoles);

	Text = "Video Collection";

        XmlDocument xdVideos = new XmlDocument();
	xdVideos.Load("../../Videos.xml");

	XmlElement xeVideo = xdVideos.DocumentElement;
	XmlNodeList xnlVideos =
		xeVideo.SelectNodes("/videos/video/cast-members/actor[@role]");

	foreach(XmlNode xnVideo in xnlVideos)
	    lbxActorsRoles.Items.Add(xnVideo.OuterXml);

	Size = new System.Drawing.Size(358, 280);
    }


    public static int Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
	    Application.Run(new Exercise());

      	return 0;
    }
}

This would produce:

Getting an Attribute and its Parent

If you want to get all attributes and their parent, remember that you can use * in place of the name of the attribute. Here is an example:

using System;
using System.Xml;
using System.Drawing;
using System.Windows.Forms;

public class Exercise : Form
{
    Label   lblActorsRoles;
    ListBox lbxActorsRoles;

    public Exercise()
    {
	lblActorsRoles = new Label();
	lblActorsRoles.AutoSize = true;
	lblActorsRoles.Text = "Characters in Videos";
	lblActorsRoles.Location = new Point(12, 10);
	Controls.Add(lblActorsRoles);

	lbxActorsRoles = new ListBox();
	lbxActorsRoles.Size = new System.Drawing.Size(655, 50);
	lbxActorsRoles.Location = new Point(12, 28);
	Controls.Add(lbxActorsRoles);

	Text = "Video Collection";

        XmlDocument xdVideos = new XmlDocument();

	xdVideos.Load("../../Videos.xml");

	XmlElement xeVideo = xdVideos.DocumentElement;
	XmlNodeList xnlVideos = xeVideo.SelectNodes("/videos/video/title[@*]");

	foreach(XmlNode xnVideo in xnlVideos)
	    lbxActorsRoles.Items.Add(xnVideo.OuterXml);

	Size = new System.Drawing.Size(688, 110);
    }


    public static int Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
    	Application.Run(new Exercise());

    	return 0;
    }
}

This would produce:

Getting an Attribute and its Parent


Previous Copyright © 2014-2021, FunctionX, Inc. Next