Linq to XML - remove the node and add a new node in the same place

I have an XDocument and I need to remove the node and add the same node again after some manipulations (my xelement node is complex and has internal nodes). Someone got a good way to do this, as my new managed node is added at the very end of xmldocument. Any snippets of code would be appreciated.

+5
source share
3 answers

If you are simply editing a node, then why delete it at all? Just get a link to it in the tree and edit it in place.

If for some reason this is not an option, then one way to do it is: if you find XElement(or, in general XNode), you need to replace in the tree, create a new one XElementto replace, and then use the XNode.ReplaceWithmethod for the old element, passing the new one as argument.

+4
source

If you understand correctly, this will help you do it.

SolarSystem.xml:

<?xml version="1.0" encoding="UTF-8"?>
<SolarSystem>
  <Planets>
    <Planet Id="1">
      <Name>Mercury</Name>
    </Planet>
    <Planet Id="2">
      <Name>Venus</Name>
    </Planet>
    <Planet Id="3">
      <Name>Earth</Name>
    </Planet>
  </Planets>
</SolarSystem>

The code finds <Planet>Mercury, adds an additional element to it, deletes it, and inserts it at the end of the collection <Planets>.

XDocument SolarSystem = XDocument.Load(Server.MapPath("SolarSystem.xml"));
IEnumerable<XElement> Planets = SolarSystem.Element("SolarSystem").Element("Planets").Elements("Planet");

// identify and change Mercury
XElement Mercury = Planets.Where(p => p.Attribute("Id").Value == "1").FirstOrDefault();
Mercury.Add(new XElement("YearLengthInDays", "88"));

// remove Mercury from current position, and add back in at the end
Mercury.Remove();
Planets.Last().AddAfterSelf(Mercury);

// save it as new file
SolarSystem.Save(Server.MapPath("NewSolarSystem.xml"));

which gives:

<?xml version="1.0" encoding="UTF-8"?>
   <SolarSystem>
     <Planets>
       <Planet Id="2">
         <Name>Venus</Name>
       </Planet>
       <Planet Id="3">
         <Name>Earth</Name>
       </Planet>
       <Planet Id="1">
         <Name>Mercury</Name>
         <YearLengthInDays>88</YearLengthInDays>
       </Planet>
     </Planets>
   </SolarSystem>
+8
source

@Ralph Lavelle. , . , . XML , Server.MapPath(), , . :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;

class LinqDemo
{
static void Main( )
    {
        XDocument SolarSystem = XDocument.Load("SolarSystem.xml");
        IEnumerable<XElement> Planets = SolarSystem.Element("SolarSystem").Element("Planets").Elements("Planet");

        // identify and change Mercury
        XElement Mercury = Planets.Where(p => p.Attribute("Id").Value == "1").FirstOrDefault();
        Mercury.Add(new XElement("YearLengthInDays", "88"));

        // remove Mercury from current position, and add back in at the end
        Mercury.Remove();
        Planets.Last().AddAfterSelf(Mercury);

        // save it as new file
        SolarSystem.Save("NewSolarSystem.xml");
    }
}

, LINQ noob, .

+1