You tried to insert an element (actually it has to be an XmlNode) using InsertAfter() or InsertBefore() method as doing so results in an exception with "The reference node is not a child of this node."
Solution Example:
The following code example demonstrates how InsertAfter() should (or at least could) be used, then add yet another node from another XmlDocument (created in-line in the code).
XmlDocument Insertion Example
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.XPath;
namespace XMLDocumentExample
{
class Program
{
static void Main(string[] args)
{
// === EXAMPLE 1: How To Use InsertAfter in XMLDocuments ========================
XmlDocument doc1 = new XmlDocument();
// This XPathNavigator creates a convenient way to Write out the tree on the console.
// we don't use it for nothing else in this example.
XPathNavigator nav1 = doc1.CreateNavigator();
// This creates the root document, which all XML documents will need.
// i.e. <doc1></doc1>
XmlElement e = doc1.CreateElement("doc1");
doc1.AppendChild(e);
// Everything in thid document are the child of the root. I will purposely make a
// Mistake here so that I don't have doc1.2.0 element. I will demonstrate inserting
// doc1.2.0 using InsertAfter.
/*
*
* <doc1>
<doc1.0.0 />
<doc1.1.0 />
<doc1.3.0 />
</doc1>
*
*/
XmlElement c = doc1.CreateElement("doc1.0.0");
e.AppendChild(c);
c = doc1.CreateElement("doc1.1.0");
e.AppendChild(c);
c = doc1.CreateElement("doc1.3.0");
e.AppendChild(c);
Console.WriteLine(nav1.OuterXml); // Will show you you have doc1.0.0, doc1.1.0 and doc1.3.0 now.
// Now we will try to add the missing doc1.2.0 and just as a bonus I will attach doc1.2.1 as the
// child of doc1.2.0
// First let's get to the node AFTER where we will insert doc1.2.0, so that will be doc1.1.0
// Note you can either use /doc1/doc1.1.0 or //doc1.1.0 as the XPath query.
XmlNode nod1_1_0 = doc1.SelectSingleNode("//doc1.1.0");
// This block of code assembles doc1.2.0 node and its child doc1.2.1
XmlElement n2 = doc1.CreateElement("doc1.2.0");
XmlElement n2c = doc1.CreateElement("doc1.2.1");
n2.AppendChild(n2c);
// This is the most tricky important part. You have to ask the parent node to InsertAfter, and not
// directly with doc1.
//XmlNode myParent = nod1_1_0.ParentNode;
//myParent.InsertAfter(n2, nod1_1_0);
XmlNode mp = doc1.FirstChild;
mp.InsertAfter(n2, nod1_1_0);
// The result would look like this:
/*
<doc1>
<doc1.0.0 />
<doc1.1.0 />
<doc1.2.0>
<doc1.2.1 />
</doc1.2.0>
<doc1.3.0 />
</doc1>
* */
// Write the tree out on the console.
Console.WriteLine(nav1.OuterXml);
// === EXAMPLE 2: Copy A Node from One XMLDocument to Another ==============
// Create the Second XmlDocument, and then copy a node in the second
// document into the first document.
XmlDocument doc2 = new XmlDocument();
doc2.AppendChild(doc2.CreateElement("doc2"));
doc2.FirstChild.AppendChild(doc2.CreateElement("doc2.0.0"));
XPathNavigator nav2 = doc2.CreateNavigator();
Console.WriteLine(nav2.OuterXml);
XmlNode node_2_0_0 = doc2.SelectSingleNode("//doc2.0.0");
// Note that this does not append the node, it simply says that
// a foreign node can now belong to the first document.
XmlNode importableNode = doc1.ImportNode(node_2_0_0, true);
doc1.SelectSingleNode("/doc1/").AppendChild(importableNode);
Console.WriteLine(nav1.OuterXml);
Console.ReadLine();
}
}
}
*Note: Cloning of a node is required when you need to make a copy of a node and insert that back into the same XmlDocument. It appears that you cannot make a reference to a node and insert that (of course, if you can do that then there is a side effect of both node data changing if you edit the content of one node.)
