Ben wrote:
Can anyone tell me how to move a node up or down in an xml document? For
example:
<root>
<field name="1"/>
<field name="2"/>
<field name="3"/>
</root>
I would like to move the field node name="3" up one position.
DOM manipulation with InsertBefore can do that e.g.
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.PreserveWhitespace = false;
xmlDocument.Load(@"file.xml");
XmlNode fieldNode = xmlDocument.SelectSingleNode(
@"/root/field[@name = '3']");
if (fieldNode != null) {
fieldNode.ParentNode.InsertBefore(fieldNode,
fieldNode.PreviousSibling);
}
// save changes somewhere
// in this example simply the console to show changes
xmlDocument.Save(Console.Out);
Note that white space can be an issue when deciding what "up one
position" means.
--
Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/