473,386 Members | 1,654 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Moving an XML node up or Down

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

Jan 6 '06 #1
3 5817

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


Jan 7 '06 #2


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/
Jan 7 '06 #3
Ben
Hi Martin,

Your code sample worked great! Thanks!

Ben

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

Jan 9 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Ha | last post by:
I am trying to move a node of a tree up one level. Example: I want to move C to be a child of A, then remove B. Before: <A> <B> <C> <D/> <E/> </C>
5
by: Claudio Jolowicz | last post by:
Suppose you have this document: <root> <node id="1"> <node id="4"/> <node id="5"/> </node> <node id="2"/> <node id="3"/> </root>
2
by: Scott Simpson | last post by:
Can you query from a non-root node using XPathAPI's function selectNodeList(Node contextNode, java.lang.String str)? I'm trying this using the XPath expression "//*" and I'm getting nothing back. I...
8
by: Ryan Stewart | last post by:
Putting the following code in a page seems to make it go into an infinite loop unless you give it a very simple node to work with. Either that or it's very very slow. I'm somewhat new to this,...
0
by: Victor | last post by:
I need to move a node to different position at the same level. Internet explorer does very intiutive using a line where the node should be inserted: -How a line may be presented between nodes...
3
by: UJ | last post by:
I've got a working web service that we are moving to another machine and now I suddenly get the following error. While transferring the files, got the following message:...
5
by: PeterBraden1 | last post by:
Hi, I have recently been learning python, and coming from a java background there are many new ways of doing things that I am only just getting to grips with. I wondered if anyone could take...
0
by: Malcolm Dew-Jones | last post by:
I have a test= that works but I would like to figure out how to move it into a match=. (Ok, that's unclear but pls keep reading.) Part of my XSLT file looks like this. <xsl:template...
6
Sl1ver
by: Sl1ver | last post by:
I've got a problem, i got the nodes to move but 1. they copy nodes(if you drag it to 3 different places it will actually have 3 of the same nodes) 2. i want to make the nodes, if moved update the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.