CSharp - Adding a Node in a Specific Location of the Specified Node's Child Nodes with AddAfterSelf

Description

Adding a Node in a Specific Location of the Specified Node's Child Nodes with AddAfterSelf

Demo

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

class Program//w  w  w . ja v  a  2s. co  m
{
    static void Main(string[] args){
             //  A document with one book participant.
             XDocument xDocument = new XDocument(
               new XElement("Books",
                 new XElement("Book",
                   new XAttribute("type", "Author"),
                   new XElement("FirstName", "Joe"),
                   new XElement("LastName", "Ruby"))));
        
             xDocument.Element("Books").Add(
               new XElement("Book",
                 new XAttribute("type", "Editor"),
                 new XElement("FirstName", "PHP"),
                 new XElement("LastName", "Python")));
        
             xDocument.Element("Books").
               Element("Book").AddAfterSelf(
            new XElement("Book",
              new XAttribute("type", "Technical Reviewer"),
              new XElement("FirstName", "Fabio"),
              new XElement("LastName", "Ferracchiati")));
        
        Console.WriteLine(xDocument);
    }
}

Result

Related Topic