Introduction to XML Elements
Introduction to XML Elements
Elements Fundamentals
Introduction
Consider an XML file as follows:
File: videos.xml
<?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> <video> <title>Chalte Chalte</title> <director>Aziz Mirza</director> <length>145 Mins</length> <format>DVD</format> <rating>N/R</rating> </video> </videos>
An element in an XML document is an object that begins with a start-tag, may contain a value, and may terminate 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.
An XML Element
To support XML elements, the System.Xml namespace provides a class named XmlElement:
public class XmlElement : System.Xml.XmlLinkedNode
To access an XML element, you can declare a variable of type XmlElement but the main purpose of this class is to get an element from a DOM object. For this reason, the XmlElement class doesn't have a constructor you can use. Instead, and as we will learn, the other classes have methods that produce an XmlElement object you can use.
In the previous lesson, we saw that every XML file must have a root node and we mentioned that you could use the XmlDocument.DocumentElement property to access it. This property is of type XmlElement:
public System.Xml.XmlElement? DocumentElement { get; }
To access DOM, you can declare an XmlElement variable and assign it this property. Here is an example:
using System.Xml;
namespace VideoCollection
{
public partial class Exercise : Form
{
public Exercise()
{
InitializeComponent();
}
private void Exercise_Load(object sender, EventArgs e)
{
XmlDocument xdVideos = new XmlDocument();
string strVideos = Environment.SpecialFolder.Personal.ToString() + @"\Videos.xml";
if (File.Exists(strVideos))
{
xdVideos.Load(strVideos);
XmlElement xeVideo = xdVideos.DocumentElement!;
}
else
MessageBox.Show("The file " + strVideos + " was not found",
"Video Collection",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
An XML element is represented in the XmlNodeType enumeration as the Element member. When using the Read() method of an XmlTextReader object, to find out if the item being read is an element, you can check whether the member of the current XmlNodeType is Element. Here is an example:
private void btnDocument_Click(object sender, EventArgs e)
{
XmlDocument xdVideos = new();
string strVideos = @"E:\Videos Collection\Videos.xml";
if (File.Exists(strVideos))
{
XmlTextReader rdrVideos = new XmlTextReader(strVideos);
do
{
switch (rdrVideos.NodeType)
{
case XmlNodeType.Element:
break;
}
} while (rdrVideos.Read());
}
else
MessageBox.Show("The file " + strVideos + " was not found",
"Video Collection",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
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 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 a property named Name. Thist property can be used to identify an existing element. Here is an example of accessing it:
using System.Xml; namespace VideoCollection { public partial class Exercise : Form { public Exercise() { InitializeComponent(); } private void Exercise_Load(object sender, EventArgs e) { var xdVideos = new XmlDocument(); string strVideos = @"E:\Videos Collection\Videos.xml"; if (File.Exists(strVideos)) { xdVideos.Load(strVideos); XmlElement xeVideo = xdVideos.DocumentElement!; MessageBox.Show(xeVideo.Name, "Video Collection", MessageBoxButtons.OK, MessageBoxIcon.Information); } else MessageBox.Show("The file " + strVideos + " was not found", "Video Collection", MessageBoxButtons.OK, MessageBoxIcon.Information); } } }
This would produce:
Notice that "videos" is returned as the name of the root element of the file. If calling the Read() method of an XmlTextReader object to scan a file, when you get to an element, you can find out its Name identity by accessing it. Here is an example:
private void btnDocument_Click(object sender, EventArgs e) { XmlDocument xdVideos = new XmlDocument(); string strVideos = @"E:\Videos Collection\Videos.xml"; if (File.Exists(strVideos)) { XmlTextReader rdrVideos = new(strVideos); do { switch (rdrVideos.NodeType) { case XmlNodeType.Element: MessageBox.Show(rdrVideos.Name, "Video Collection", MessageBoxButtons.OK, MessageBoxIcon.Information); break; } } while (rdrVideos.Read()); } else MessageBox.Show("The file " + strVideos + " was not found", "Video Collection", MessageBoxButtons.OK, MessageBoxIcon.Information); }
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"?> <videos> <video> <title>The Distinguished Gentleman</title> <director>Jonathan Lynn</director> <length-in-minutes>112</length-in-minutes> <format>DVD</format> <rating>R</rating> <price>14.95</price> </video> <video> <title>Her Alibi</title> <director>Bruce Beresford</director> <length-in-minutes>94</length-in-minutes> <format>VHS</format> <rating>PG-13</rating> <price>9.95</price> </video> </videos>
Notice that the price elements contain numbers that look like currency values and the length-in-minutes elements use an integer as value.
If you are using an XmlTextReader object to scan a file, when the Read() method gets to an element, you can find out what its value is by accessing this property. Here is an example:
private void btnDocument_Click(object sender, EventArgs e)
{
XmlDocument xdVideos = new XmlDocument();
string strVideos = @"E:\Videos Collection\Videos.xml";
if (File.Exists(strVideos))
{
var rdrVideos = new XmlTextReader(strVideos);
do
{
switch (rdrVideos.NodeType)
{
case XmlNodeType.Text:
MessageBox.Show(rdrVideos.Value,
"Video Collection",
MessageBoxButtons.OK, MessageBoxIcon.Information);
break;
}
} while (rdrVideos.Read());
}
else
MessageBox.Show("The file " + strVideos + " was not found",
"Video Collection",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
The value or text of an element is an object of type XmlText.
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. When a tag is empty, the Value property of its XmlElement object would return an empty value.
Character Entities in an Element Value
Besides the 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>
<full-name>Sylvie <Bellie> Aronson</full-name>
<salary>25.64</salary>
<department-id>1</department-id>
</employee>
<employee>
<full-name>Bertrand Yamaguchi</full-name>
<salary>16.38</salary>
<department-id>4</department-id>
</employee>
</employees>
If you try using this XML document, for example, if you try displaying it in a browser, you would receive an error:
The reason is that when the parser reaches the <full-name>Sylvie <Bellie> Aronson</full-name> 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. For example, the < (less than) character is represented with < and the > (greater than) symbol can be used with >. Therefore, the above code can be corrected as follows:
<?xml version="1.0" encoding="utf-8" ?> <employees> <employee> <full-name>Sylvie <Bellie> Aronson</full-name> <salary>25.64</salary> <department-id>1</department-id> </employee> <employee> <full-name>Bertrand Yamaguchi</full-name> <salary>16.38</salary> <department-id>4</department-id> </employee> </employees>
This would produce:
Here is a list of other codes you can use for special characters:
Code | Symbol | Code | Symbol | Code | Symbol | Code | Symbol | Code | Symbol |
' | ' | C | C | j | j | ³ | ³ | Ú | Ú |
< | < | D | D | k | k | ´ | ´ | Û | Û |
> | > | E | E | l | l | µ | µ | Ü | Ü |
& | & | F | F | m | m | ¶ | ¶ | Ý | Ý |
" | " | G | G | n | n | · | · | Þ | Þ |
! | ! | H | H | o | o | ¸ | ¸ | ß | ß |
" | " | I | I | p | p | ¹ | ¹ | à | à |
# | # | J | J | q | q | º | º | á | á |
$ | $ | K | K | r | r | » | » | â | â |
% | % | L | L | s | s | ¼ | ¼ | ã | ã |
& | & | M | M | t | t | ½ | ½ | ä | ä |
' | ' | N | N | u | u | ¾ | ¾ | å | å |
( | ( | O | O | v | v | ¿ | ¿ | æ | æ |
) | ) | P | P | w | w | À | À | ç | ç |
* | * | Q | Q | x | x | Á | Á | è | è |
+ | + | R | R | y | y | Â | Â | é | é |
, | , | S | S | z | z | Ã | Ã | ê | ê |
- | - | T | T | { | { | Ä | Ä | ë | ë |
. | . | U | U | } | } | Å | Å | ì | ì |
/ | / | V | V | ~ | ~ | Æ | Æ | í | í |
0 | 0 | W | W |   | empty | Ç | Ç | î | î |
1 | 1 | X | X | ¡ | ¡ | È | È | ï | ï |
2 | 2 | Y | Y | ¢ | ¢ | É | É | ð | ð |
3 | 3 | Z | Z | £ | £ | Ê | Ê | ñ | ñ |
4 | 4 | [ | [ | ¤ | ¤ | Ë | Ë | ò | ò |
5 | 5 | \ | \ | ¥ | ¥ | Ì | Ì | ó | ó |
6 | 6 | ] | ] | ¦ | ¦ | Í | Í | ô | ô |
7 | 7 | ^ | ^ | § | § | Î | Î | õ | õ |
8 | 8 | _ | _ | ¨ | ¨ | Ï | Ï | ö | ö |
9 | 9 | ` | ` | © | © | Ð | Ð | ÷ | ÷ |
: | : | a | a | ª | ª | Ñ | Ñ | ø | ø |
; | ; | b | b | « | « | Ò | Ò | ù | ù |
< | < | c | c | ¬ | ¬ | Ó | Ó | ú | ú |
= | = | d | d | ­ | | Ô | Ô | û | û |
> | > | e | e | ® | ® | Õ | Õ | ü | ü |
? | ? | f | f | ¯ | ¯ | Ö | Ö | ý | ý |
@ | @ | g | g | ° | ° | × | × | þ | þ |
A | A | h | h | ± | ± | Ø | Ø | ÿ | ÿ |
B | B | i | i | ² | ² | Ù | Ù | Ā | Ā |
There are still other codes to include special characters in an XML file.
Identifying the Markup of a Node
The Inner Text of a node
In the previous sections, we have seen how to create a tag to produce a node. We also saw that a node could be placed inside of another node. 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 virtual string InnerText { get; set; }
This property concatenates the values of the children of the node that called them but doesn't include their markups. Here is an example:
using System.Xml;
public class Exercise : Form
{
public Exercise()
{
InitializeComponent();
}
private void btnDocument_Click(object sender, EventArgs e)
{
XmlDocument xdVideos = new XmlDocument();
string strVideos = @"E:\Videos Collection\Videos.xml";
if (File.Exists(strVideos))
{
xdVideos.Load(strVideos);
XmlElement xeVideo = xdVideos.DocumentElement!;
rtbDocument.Text = xeVideo.InnerText;
}
else
MessageBox.Show("The file " + strVideos + " was not found",
"Video Collection",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
This would produce:
Notice that this property produces all values of the children of a node in one block. We already saw how to access each value of the children of a node by calling the XmlTextReader.Read() method and get its Text.
The Outer XML Code of a Node
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 virtual string OuterXml { get; }
Here is an example:
using System.Xml;
public class Exercise : Form
{
private void btnDocument_Click(object sender, EventArgs e)
{
XmlDocument xdVideos = new XmlDocument();
string strVideos = @"E:\Videos Collection\Videos.xml";
if (File.Exists(strVideos))
{
xdVideos.Load(strVideos);
XmlElement xeVideo = xdVideos.DocumentElement!;
rtbDocument.Text = elm.OuterXml;
}
else
MessageBox.Show("The file " + strVideos + " was not found",
"Video Collection",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
This would produce:
The Inner XML Code of a Node
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 virtual string InnerXml { get; set; }
Here is an example:
private void btnDocument_Click(object sender, EventArgs e)
{
XmlDocument xdVideos = new XmlDocument();
string strVideos = @"E:\Videos Collection\Videos.xml";
if (File.Exists(strVideos))
{
xdVideos.Load(strVideos);
XmlElement xeVideo = xdVideos.DocumentElement!;
rtbDocument.Text = elm.InnerXml;
}
else
MessageBox.Show("The file " + strVideos + " was not found",
"Video Collection",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
This would produce:
The Child Nodes of a Node
Introduction
As mentioned already, one node can be nested inside of another. A nested node is called a child of the nesting node. This also implies that a node can have as many children as necessary, making them child nodes of the parent node. Once again, consider our videos.xml 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> <video> <title>Chalte Chalte</title> <director>Aziz Mirza</director> <length>145 Mins</length> <format>DVD</format> <rating>N/R</rating> </video> </videos>
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 nodes.
A Collection of XML Nodes
As you may know already, an XML document usually contains many nodes. To consider a collection of nodes, the .NET Framework provides a class named XmlNodeList. It starts as follows:
public abstract class XmlNodeList : IEnumerable, IDisposable
We will review the characteristics of this class in later sections.
Linking the Nodes
To manage the relationships among the nodes of an XML document, the .NET Framework provides a class named XmlLinkedNode:
public abstract class XmlLinkedNode : XmlNode
As a result, many classes derive from XmlLinkedNode. That's the case for the XmlElement class:
public class XmlElement : XmlLinkedNode
A Collection of Child Nodes
To support the child nodes of a particular node, the XmlNode class is equipped with a property named ChildNodes. That property is of type XmlNodeList. The property is declared as follows:
public virtual XmlNodeList ChildNodes { get; }
When this property is used, it produces an XmlNodeList list, which is a collection of all nodes that share the same parent. Each item in the collection is of type XmlNode. To give you the number of nodes on an XmlNodeList collection, the class is equipped with a property named Count. Here is an example of using it:
private void btnDocument_Click(object sender, EventArgs e) { XmlDocument xdVideos = new XmlDocument(); string strVideos = @"E:\Videos Collection\Videos.xml"; if (File.Exists(strVideos)) { xdVideos.Load(strVideos); XmlElement xeVideo = xdVideos.DocumentElement!; XmlNodeList lstVideos = xeVideo.ChildNodes; MessageBox.Show("The root element contains " + lstVideos.Count + " nodes", "Video Collection", MessageBoxButtons.OK, MessageBoxIcon.Information); } else MessageBox.Show("The file " + strVideos + " was not found", "Video Collection", MessageBoxButtons.OK, MessageBoxIcon.Information); }
This would produce:
You can also use the Count property in a for loop to visit the members of the collection.
Accessing a Node in a Collection
The children of a node, that is, the members of a ChildNodes property, or the members of an XmlNodeList collection, can be located each by an index. The first node has an index of 0, the second has an index of 1, an so on. To give you access to a node of the collection, the XmlNodeList class is equipped with an indexed property and a method named Item. Both produce the same result. For example, if a node has three children, to access the third, you can apply an index of 2 to its indexed property. Here is an example:
private void btnDocument_Click(object sender, EventArgs e)
{
XmlDocument xdVideos = new XmlDocument();
string strVideos = @"E:\Videos Collection\Videos.xml";
if (File.Exists(strVideos))
{
xdVideos.Load(strVideos);
XmlElement xeVideo = xdVideos.DocumentElement!;
XmlNodeList lstVideos = xeVideo.ChildNodes;
MessageBox.Show(lstVideos[2]?.InnerText,
"Video Collection",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
MessageBox.Show("The file " + strVideos + " was not found",
"Video Collection",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
You can also use the Item() method to get the same result. Using a for loop, you can access each node and display the values of its children as follows:
private void btnDocument_Click(object sender, EventArgs e) { XmlDocument xdVideos = new XmlDocument(); string strVideos = @"E:\Videos Collection\Videos.xml"; if (File.Exists(strVideos)) { xdVideos.Load(strVideos); XmlElement xeVideo = xdVideos.DocumentElement!; XmlNodeList lstVideos = xeVideo.ChildNodes; for (int i = 0; i < lstVideos.Count; i++) MessageBox.Show(lstVideos[i]?.InnerText, "Video Collection", MessageBoxButtons.OK, MessageBoxIcon.Information); } else MessageBox.Show("The file " + strVideos + " was not found", "Video Collection", MessageBoxButtons.OK, MessageBoxIcon.Information); }
Instead of using the indexed property, the XmlNodeList class implements the IEnumerable interface. This allows you to use a foreach loop to visit each node of the collection. Here is an example:
private void btnDocument_Click(object sender, EventArgs e) { XmlDocument xdVideos = new XmlDocument(); string strVideos = @"E:\Videos Collection\Videos.xml"; if (File.Exists(strVideos)) { xdVideos.Load(strVideos); XmlElement xeVideo = xdVideos.DocumentElement!; XmlNodeList lstVideos = xeVideo.ChildNodes; foreach (XmlNode node in lstVideos) MessageBox.Show(node.InnerText, "Video Collection", MessageBoxButtons.OK, MessageBoxIcon.Information); } else MessageBox.Show("The file " + strVideos + " was not found", "Video Collection", MessageBoxButtons.OK, MessageBoxIcon.Information); }
To better manage and manipulate the nodes of a collection of nodes, you must be able to access the desired node. The XmlNode class combined with the XmlNodeList class provide various means of getting to a node and taking the appropriate actions.
The Parent of a Node
Not all nodes have children, obviously. For example, the title node of our videos.xml file does not have children. To find out whether a node has children, check its HasChildNodes Boolean property that is declared as follows:
public virtual bool HasChildNodes { get; }
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. For our videos.xml file, the first line is called the first child of the DOM. This would be:
<?xml version="1.0" encoding="utf-8"?>
After identifying or locating a node, the first node that immediately follows it is referred to as its first child. In our videos.xml file, the first child of the first video node is the <title>The Distinguished Gentleman</title> element. The first child of the second <video> node is <title>Her Alibi</title>.
In the .NET Framework, the first child of a node can be retrieved by accessing the XmlNode.FirstChild property declared as follows:
public virtual XmlNode FirstChild { get; }
In the following example, every time the parser gets to a video node, it displays the value of it first child:
using System.Xml;
public class Exercise : Form
{
public Exercise()
{
InitializeComponent();
}
private void btnDocument_Click(object sender, EventArgs e)
{
XmlDocument xdVideos = new XmlDocument();
string strVideos = @"E:\Videos Collection\Videos.xml";
if (File.Exists(strVideos))
{
xdVideos.Load(strVideos);
XmlElement xeVideo = xdVideos.DocumentElement!;
XmlNodeList lstVideos = xeVideo.ChildNodes;
foreach (XmlNode node in lstVideos)
lbxVideos.Items.Add(node.FirstChild!.InnerText);
}
else
MessageBox.Show("The file " + strVideos + " was not found",
"Video Collection",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
This would produce:
In this example, we started our parsing on the root node of the document. At times, you will need to consider only a particular node, such as the first child of a node. For example, you may want to use only the first child of the root. To get it, you can access the FirstChild property of the DocumentElement object of the DOM. Once you get that node, you can then do what you judge necessary. In the following example, only the values of the child nodes of the first child of the root are displayed:
private void btnDocument_Click(object sender, EventArgs e) { var xdVideos = new XmlDocument(); string strVideos = @"E:\Videos Collection\Videos.xml"; if (File.Exists(strVideos)) { xdVideos.Load(strVideos); XmlNode node = xdVideos.DocumentElement!.FirstChild!; foreach (XmlNode child in node.ChildNodes) lbxVideos.Items.Add(child.InnerText); } else MessageBox.Show("The file " + strVideos + " was not found", "Video Collection", MessageBoxButtons.OK, MessageBoxIcon.Information); }
This would produce:
Consider the following modification of the Videos.xml file:
<?xml version="1.0" encoding="utf-8" ?>
<videos>
<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>
<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>
<video>
<title>Chalte Chalte</title>
<director>Aziz Mirza</director>
<length>145 Mins</length>
<format>DVD</format>
<rating>N/R</rating>
</video>
</videos>
Remember that when using a for or a foreach loops applied to an XmlNodeList collection, each node that you access is a complete XmlNode object and may have children. This means that you can further get the ChildNodes node of any node. Here is an example that primarily scans the nodes but looks for one whose name is cast-members:
private void btnDocument_Click(object sender, EventArgs e) { XmlDocument xdVideos = new(); string strVideos = @"E:\Videos Collection\Videos.xml"; if (File.Exists(strVideos)) { xdVideos.Load(strVideos); // Locate the root node and // get a reference to its first child XmlNode node = xdVideos.DocumentElement!.FirstChild!; // Create a list of the child nodes of // the first node under the root XmlNodeList lstVideos = node.ChildNodes; // Visit each node for (int i = 0; i < lstVideos.Count; i++) { // Look for a node named cast-members if (lstVideos[i]?.Name == "cast-members") { // Once/if you find it, // 1. Access its first child // 2. Create a list of its child nodes XmlNodeList lstActors = lstVideos[i]?.ChildNodes!; // Display the values of the nodes for (int j = 0; j < lstActors.Count; j++) lbxVideos.Items.Add(lstActors[j]!.InnerText); } } } else MessageBox.Show("The file " + strVideos + " was not found", "Video Collection", MessageBoxButtons.OK, MessageBoxIcon.Information); }
This would produce:
As we have learned that a node or a group of nodes can be nested inside of another node. When you get to a node, you may know or find out that it has children. You may then want to consider only the first child. Here is an example:
private void btnDocument_Click(object sender, EventArgs e)
{
string strVideos = @"E:\Videos Collection\Videos.xml";
XmlDocument xdVideos = new XmlDocument();
if (File.Exists(strVideos))
{
xdVideos.Load(strVideos);
// Locate the root node and
// get a reference to its first child
XmlNode node = xdVideos.DocumentElement?.FirstChild!;
// Create a list of the child nodes of
// the first node under the root
XmlNodeList lstVideos = node.ChildNodes;
// Visit each node
for (int i = 0; i < lstVideos.Count; i++)
{
// Look for a node named cast-members
if (lstVideos[i]?.Name == "cast-members")
{
// Once/if you find it,
// 1. Access its first child
// 2. Create a list of its child nodes
XmlNodeList lstActors =
lstVideos[i]?.FirstChild?.ChildNodes!;
// Display the value of its first child node
for (int j = 0; j < lstActors.Count; j++)
lbxVideos.Items.Add(lstActors[j]?.InnerText!);
}
}
}
else
MessageBox.Show("The file " + strVideos + " was not found",
"Video Collection",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
This would produce:
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 virtual XmlNode LastChild { get; }
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 above file: Director, cast-members, and Length are child nodes of the Video node but the actor node is not a child of the Video node. Consequently, Director, cast-members, and Length are siblings. Obviously, to get a sibling, you must first have a node.
To access the sibling of a node, you can use its XmlNode.NextSibling property, which is declared as follows:
public virtual XmlNode NextSibling { get; }
Previous | Copyright © 2005-2023, FunctionX | Monday 06 December 2021 | Next |