Home All Groups Group Topic Archive Search About

Moving an XML node up or Down

Author
6 Jan 2006 8:49 PM
Ben
Hi,

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.

TIA

Ben

Author
6 Jan 2006 9:41 PM
sloan
You might want to do a search for

Xml to Xml Transformation

...

This takes one Xml, uses an Xsl, to make another Xml (as opposed to an
output html file).

That might be the hint you're looking for.

...



Ben wrote:
Show quoteHide quote
> Hi,
>
> 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.
>
> TIA
>
> Ben
Are all your drivers up to date? click for free checkup

Author
7 Jan 2006 1:28 PM
Martin Honnen
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/
Author
9 Jan 2006 1:24 PM
Ben
Hi Martin,

Your code sample worked great! Thanks!

Ben

Show quoteHide quote
"Martin Honnen" wrote:

>
>
> 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/
>

Bookmark and Share