|
.net
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Moving an XML node up or DownHi,
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 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 Ben wrote:
> Can anyone tell me how to move a node up or down in an xml document? For DOM manipulation with InsertBefore can do that e.g.> 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. 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. 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/ >
Other interesting topics
serialized binary files vs Sql Server Performance..
VS2005 - Unable to open local iis site other than localhost Firefox doesn't display ASP.NET Output a Grid (Table) from a Collection? Printing to a local printer Data Sources for ASP.Net Using ToolTip to store data Warn user that session expired? Cache per Thread Root URL/ Application path problem with running debugger |
|||||||||||||||||||||||