Operations on XML Elements


Adding an Element in a Grid Sheet

The easiest way to add text to an element, whether the element exists already or you want to add a new element, consists of manually opening the file and typing the text of the new element in the desired section. For example, you can click the Data button of the XML while opened in Visual Studio .NET:

XML Data

If you click the empty cells and fill them up, if you save the file, the new elements would be added to the file.

Practical Learning Practical Learning: Adding Elements in the Data Grid

  1. Display the Employees.xml file and click its Data button
  2. Click the first empty cell under the EmplNumber column and type 24-705
  3. Click the first empty cell under the FirstName column and type Gertrude
  4. Press Tab and type Monay
  5. Press Tab and type 14.66 and press Enter
  6. Press Ctrl + S to save the file
  7. Click the XML button and notice the new record has been added

Adding a Simple Element

To programmatically create a new element, you can declare a pointer to XmlElement. To add the new element to the file, the XmlDocument class provides the CreateElement() method that is overloaded with three versions. One of the versions uses the following syntax:

public: XmlElement* CreateElement(String* name);

Using this method, to create a new element, call it and pass it the name of the new element. If you want the element to have a value, the XmlDocument class is equipped with the CreateTextNode() method. This method returns an XmlText value. The syntax of this method is:

public: virtual XmlText* CreateTextNode(String* text);

This method takes as argument the string that would constitute the value of the element. The calls to XmlDocument::CreateElement() and XmlDocument::CreateTextNode() methods allow you to initialize a new element. In order to actually add the new element to the file, you must specify its position in the tree: whether it would be the first or the last node, whether you want to position it before or after a node of your choice. To support the positions of existing nodes, the XmlNode class, which is the ancestor of all XML nodes of the .NET Framework, including the XmlElement class, provides various appropriate methods. One of these methods is AppendChild(), which is used to add an element as the last child of its parent. The syntax of the XmlNode::AppendChild() method is:

public: virtual XmlNode* AppendChild(XmlNode* newChild);

Imagine you have a simple XML file that has only the root and one level of elements as follows:

<?xml version="1.0" encoding="utf-8"?>
  <Holiday>New Year's Day</Holiday>
  <Holiday>Birthday of Martin Luther King, Jr.</Holiday>
  <Holiday>Washington's Birthday</Holiday>

To add a new Holiday element to this file, based on the above discussions, you can use code as follows:

System::Void button1_Click(System::Object *  sender, System::EventArgs *  e)
	 XmlDocument *docXML = new XmlDocument;

	 XmlElement *elmXML = docXML->CreateElement(S"Holiday");
	 XmlText    *txtXML = docXML->CreateTextNode(S"Memorial Day");



Adding a Filled Child Element

The above Holidays.xml file had only one level under the root and no child element of the root had children. Imagine that you have a file as follows:

<?xml version="1.0" encoding="utf-8"?>
    <Title>The Distinguished Gentleman</Title>
    <Director>Jonathan Lynn</Director>
    <Length>112 Minutes</Length>
    <Title>Her Alibi</Title>
    <Director>Bruce Beresford</Director>
    <Length>94 Mins</Length>

Imagine that you want to add a Video element. You have a choice of adding one, more, or all child elements of the Video node. To perform this operation, one solution you can use is to "build" all child elements of the Video node, then add the node as a whole. To support this technique, we saw earlier that the XmlNode::InnerXml property comprises a node, its markup, its children and their markup. This means that you can create the child nodes with their markup(s) as a string and assign that string to an XmlNode::InnerXml string. To do this, you would need the set version of the InnerXml property. It is declared as follows:

public: __property virtual void set_InnerXml(String*);

Here is an example that adds a complete new Video node to the above XML file:

System::Void button3_Click(System::Object *  sender, System::EventArgs *  e)
	 XmlDocument *docXML = new XmlDocument;

	 XmlElement *elmXML = docXML->CreateElement(S"Video");
	 String *strNewVideo = S"<Title>Other People's Money</Title>"
		                      S"<Director>Alan Brunstein</Director>"
			      S"<Length>114 Minutes</Length>"

	 elmXML->InnerXml = strNewVideo;


If you have an XML file with different levels and you want to add an element as a new child to a particular element, you can first locate that element. One way you can do this is to call the XmlDocument::GetElementByTagName() method. This method is overloaded with two versions. One of its versions has the following syntax:

public: virtual XmlNodeList* GetElementsByTagName(String* name);

This method takes as argument the name of the element that you want to locate. If the method finds that element, it returns a list of all child nodes of that element as an XmlNodeList object. This XmlNodeList object represents a collection of nodes where each node can be located by its index through the ItemOf indexed property.

Element Navigation

An XML tree navigation consists of visiting the various nodes of the file, for any necessary reason. One reason would be that you want to insert a new element at a specific position in the tree. Another would be that you want to check whether a certain node is already present in the tree.

As always, there are various ways you can perform an operation such as navigating a tree. To "scan" a file from top to bottom, thus visiting each type of node in the XML file, you can use the XmlTextReader class. This class is equipped with all types of properties and methods you would need.

If you want to navigate to a specific node in the tree, you can use the XmlDocument::GetElementByTagName() method that we used previously.



Previous Copyright © 2004-2010 FunctionX, Inc. Next