Now that you’ve seen that you can move around in the XML document and even get the values of the
elements, let’s examine how to change the structure of the document by adding nodes to the books
document you’ve been using until now.
To insert new elements in the list, you need to examine the new methods that are placed on the
XmlDocument and XmlNode classes, shown in Table 22-4. The XmlDocument class has methods that
enable you to create new XmlNode and XmlElement instances, which is nice because both of these
classes have only a protected constructor, which means you cannot create an instance of either directly
with new.
The methods in Table 22-4 are all used to create the nodes themselves, but after calling any of them you
have to do something with them before they become interesting. Immediately after creation, the nodes
contain no additional information, and they are not yet inserted into the document. To do either, you
Using XML in Your Application ❘ 741
should use methods that are found on any class derived from XmlNode (including XmlDocument and
XmlElement), described in the following table:
METHOD DESCRIPTION
AppendChild Appends a child node to a node of type XmlNode or a derived type. Remember
that the node you append appears at the bottom of the list of children of the
node on which the method is called. If you don’t care about the order of the children,
there’s no problem; if you do care, remember to append the nodes in the
correct sequence.
InsertAfter Controls exactly where you want to insert the new node. The method takes two
parameters — the first is the new node and the second is the node after which
the new node should be inserted.
InsertBefore Works exactly like InsertAfter, except that the new node is inserted before
the node you supply as a reference.
TABLE 22-4: Methods for Creating Nodes
METHOD DESCRIPTION
CreateNode Creates any kind of node. There are three overloads of the method, two of
which enable you to create nodes of the type found in the XmlNodeType
enumeration and one that enables you to specify the type of node to use as
a string. Unless you are quite sure about specifying a node type other than
those in the enumeration, use the two overloads that use the enumeration. The
method returns an instance of XmlNode that can then be cast to the appropriate
type explicitly.
CreateElement A version of CreateNode that creates only nodes of the XmlDocument variety.
CreateAttribute A version of CreateNode that creates only nodes of the XmlAttribute variety.
CreateTextNode Creates — yes, you guessed it — nodes of the type XmlTextNode
CreateComment This method is included here to highlight the diversity of node types that can
be created. This method doesn’t create a node that is actually part of the data
represented by the XML document, but rather is a comment meant for any
human eyes that might have to read the data. You can pick up comments when
reading the document in your applications as well.
In the Following Try It Out, you build on the previous example and insert a book node in the books.xml
document. There is no code in the example to clean up the document (yet), so if you run it several times
you will probably end up with a lot of identical nodes.
742 ❘