AppendChild problem
-
I'm attempting to modify an XML Document using System.Xml's DOM methods. I'm loading a template document, then modifying it with values from my application. The final document has some repeating nodes, where the template document includes only one template node for these. I use the following technique to create the required nodes: XmlNode node = requestedMessagesElement.SelectSingleNode( "//Message"); XmlElement firstMessageElement = (XmlElement)node; // Add message nodes for each message for (long i = 0; i < numMessages; ++i) { XmlElement messageElement; // For first element, modify existing message if (i == 0) messageElement = firstMessageElement; // For successive elements, copy existing element to modify else { messageElement = (XmlElement)firstMessageElement.Clone(); messageElement = (XmlElement)requestedMessagesElement.AppendChild( messageElement); } // Other modifications here } For some reason, when I execute the code, the resulting XML misplaces the resulting nodes. For example, if the nodes are appended in order "1, 2, 3", I'm consistently getting results in order "3, 1, 2". Any suggestions greatly appreciated. PS. I use the value returned from "AppendChild" as the documentation states that the method returns the appended node - just covering tracks in case the system decides to insert a copy of the passed node for some reason.