CSharp - Deleting a Specific Node with Remove

Description

Deleting a Specific Node with Remove

Demo

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

class Program//  w ww .  jav  a 2 s  .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;
        
        Console.WriteLine(System.Environment.NewLine + "Before node deletion");
              XDocument xDocument = new XDocument(
               new XElement("Books", firstParticipant =
                 new XElement("Book",
                   new XAttribute("type", "Author"),
                   new XElement("FirstName", "Joe"),
                   new XElement("LastName", "Ruby")),
                 new XElement("Book",
                   new XAttribute("type", "Editor"),
                   new XElement("FirstName", "PHP"),
                   new XElement("LastName", "Python"))));
        
              Console.WriteLine(xDocument);
        
              firstParticipant.Remove();
              Console.WriteLine(System.Environment.NewLine + "After node deletion");
        
              Console.WriteLine(xDocument);
    }
}

Result

Related Topic