In some cases, you may want to perform an operation on
an existing and particular node. For example, you may want to change the value
of a node, you may want to add a new child node to an existing node, etc. Before
taking any of these actions, you must be able to locate or identify the desired
element.

Locating an element consists of looking for a particular
node among the nodes. To do this, you must start somewhere. Obviously, the first
node you can identify is the root. Once you get the root, you can then get a
collection of its children. After getting a collection of the children of the
root, you can locate a node in the collection. If the node you are looking for
is a child of that first collection, you can then get a collection of the child
nodes of that node and proceed.

Fortunately, the System.Xml namespace provides
various means of looking for a node in an XML file.

You can use this characteristic to locate a node.
Because XML is very flexible with the names (you can have two child nodes that
have the same name) and values (you can have two child nodes that have the same
value) of nodes, when creating an XML file, it is your responsibility to create
a scheme that would eventually allow you to uniquely identify each element.

Practical
Learning: Locating an Element Using its Index

On the form, double-click the New Cleaning Order button and implement its
event as follows:

To assist you with locating the first child node of a node, the
XmlNode class is equipped with an indexed property (named Item)
overloaded with two versions. One of the versions is declared as follows:

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

This indexed property takes the name of a node as argument.
After the property has been called, the parser checks the child nodes of the
element on which the property was applied. If the parser finds a node with that
name, it returns it as an XmlElement object. Here is an example:

To assist you with finding a node, the XmlDocument class is
equipped with the GetElementByTagName() method which is overloaded with
two versions. One of the syntaxes used is:

public virtual XmlNodeList GetElementsByTagName(string name);

This method takes as argument a string. The string must be
the name of a node. If at least one node that holds that name exists in the
file, this method returns a collection of the nodes with that name. If there is
no node with that name, the collection is returned empty and there is no
exception thrown.

Imagine you want to add a list of actors of the Her Alibi
video. The first action to take is to locate the video, which you can do by
calling the XmlDocument.GetElementsByTagName() method applied to a
collection of nodes whose names are video. From this list of nodes, you can look
for the node whose value is "Her Alibi". Once you have found this
element, get a reference to its parent. Then add the new node as a child its parent. This can be done as follows:

This code creates an empty element. If you want to create an
element that includes a value, create its text and add that text to the node. Here is an example:

private void btnDocument_Click(object sender, EventArgs e)
{
string Filename = "videos.xml";
XmlDocument xmlDoc = new XmlDocument();
if (File.Exists(Filename))
{
xmlDoc.Load(Filename);
// Get a reference to the root node
XmlElement elmRoot = xmlDoc.DocumentElement;
// Create a list of nodes whose name is Title
XmlNodeList lstTitles = xmlDoc.GetElementsByTagName("title");
// Now you can check each node of the list
foreach (XmlNode node in lstTitles)
{
// When you get to a node, look for the element's value
// If you find an element whose value is Her Alibi
if (node.InnerText == "The Distinguished Gentleman")
{
// Create an element named Category
XmlElement elmNew = xmlDoc.CreateElement("category");
// Create the text of the new element
XmlText txtCatetory = xmlDoc.CreateTextNode("Comedy");
// Get a reference to the parent of the node we have found
XmlNode elmParent = node.ParentNode;
// Add the new element to the node we found
elmParent.AppendChild(elmNew);
// Specify the text of the new node
elmParent.LastChild.AppendChild(txtCatetory);
// Save the file
xmlDoc.Save(Filename);
}
}
}
}

Using the same approach combined with what we learned about
adding an item, you can add a new element that itself has child nodes. Here is
an example:

private void btnDocument_Click(object sender, EventArgs e)
{
string Filename = "videos.xml";
XmlDocument xmlDoc = new XmlDocument();
if (File.Exists(Filename))
{
xmlDoc.Load(Filename);
// Get a reference to the root node
XmlElement elmRoot = xmlDoc.DocumentElement;
// Create a list of nodes whose name is Title
XmlNodeList lstTitles = xmlDoc.GetElementsByTagName("title");
// Now you can check each node of the list
foreach (XmlNode node in lstTitles)
{
// When you get to a node, look for the element's value
// If you find an element whose value is The Day After Tomorrow
if (node.InnerText == "The Day After Tomorrow")
{
// Create an element named Actors
XmlElement elmNew = xmlDoc.CreateElement("actors");
// Get a reference to the parent of the node we have found
XmlNode elmVideo = node.ParentNode;
// Add the new element to the node we found
elmVideo.AppendChild(elmNew);
// Create an element as a child of the new element
// Specify its name as Actor
elmNew = xmlDoc.CreateElement("actor");
// Create the text of the new element
XmlText txtActor = xmlDoc.CreateTextNode("Dennis Quaid");
// Add the new Actor element to the Actors node
elmVideo.LastChild.AppendChild(elmNew);
// Specify the text of the new node
elmVideo.LastChild.LastChild.AppendChild(txtActor);
// In the same way, add the other Actor nodes
elmNew = xmlDoc.CreateElement("actor");
txtActor = xmlDoc.CreateTextNode("Jake Gyllenhaal");
elmVideo.LastChild.AppendChild(elmNew);
elmVideo.LastChild.LastChild.AppendChild(txtActor);
elmNew = xmlDoc.CreateElement("actor");
txtActor = xmlDoc.CreateTextNode("Emmy Rossum");
elmVideo.LastChild.AppendChild(elmNew);
elmVideo.LastChild.LastChild.AppendChild(txtActor);
elmNew = xmlDoc.CreateElement("actor");
txtActor = xmlDoc.CreateTextNode("Dash Mihok");
elmVideo.LastChild.AppendChild(elmNew);
elmVideo.LastChild.LastChild.AppendChild(txtActor);
// Save the file
xmlDoc.Save(Filename);
}
}
}
}

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

Imagine you want to create a new category element below the
director element whose name is Adrian Lyne. You can first get a list of videos.
Inside of each video, check the nodes and find out whether the video has a
director node whose text is Adrian Lyne. Once you find that node, you can add
the new element after it. Here is an example:

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

public virtual XmlNode RemoveChild(XmlNode oldChild);

This method takes as argument the node to delete. If the
node exists, it would be deleted and the method would return the node that was
deleted. If the node does not exist, nothing would happen. To effectively use this method, you should
first locate the particular node you want to delete. You can look for it using
any of the logics we have applied so far. Once you find the node, you can then
delete it. Imagine you want to delete a node whose name is Director and whose
value is Bruce Beresford. Here is an example of calling this method to perform
the operation: