-1

I have an xElement data <a><item><item1/><item2/></item></a>

Need to replace item node with another node . How can we achieve this? I just need to replace item node with some other node say <b><b1/></b>

I want output as <a><b><b1/></b></a>

Jeroen Mostert
  • 27,176
  • 2
  • 52
  • 85

2 Answers2

0

you could use ReplaceWith

xelementnode.ReplaceWith(new XElement(yournode));
Thorarins
  • 1,836
  • 16
  • 21
0

Please find the below script, this is help you to replace the node

XElement xmlTree = new XElement("Students",
    new XElement("Andrew", "Andrew Wilson"),
    new XElement("Thomas", "Thomas Alwa"),
    new XElement("Winston", "Winston GH"),
    new XElement("Hary", "Hary Potter"),
    new XElement("Jacky", "Jacky Elia")
);

XElement xel = xmlTree.Element("Winston");

xel.ReplaceWith(new XElement("Bill", "Bill Gate"));

Console.WriteLine(xmlTree);
Abhilash Thomas
  • 833
  • 2
  • 12
  • 23