CSharp - Using ReplaceAll to Change an Element's Subtree

Description

Using ReplaceAll to Change an Element's Subtree

Demo

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

class Program/*ww w . j a v a  2s .c o  m*/
{
    static void Main(string[] args){
          //  we will use this to store a reference to one of the elements in the XML tree.
          XElement firstParticipant;
    
          XDocument xDocument = new XDocument(
            new XElement("Books", firstParticipant =
              new XElement("Book",
                new XAttribute("type", "Author"),
                new XElement("FirstName", "Joe"),
                new XElement("LastName", "Ruby"))));
    
          Console.WriteLine(System.Environment.NewLine + "Before updating elements:");
          Console.WriteLine(xDocument);
    
          firstParticipant.ReplaceAll(
            new XElement("FirstName", "PHP"),
            new XElement("LastName", "Python"));
    
          Console.WriteLine(System.Environment.NewLine + "After updating elements:");
          Console.WriteLine(xDocument);
    }
}

Result

Related Topic