CSharp - Adding a Node to the End of the Specified Node's Child Nodes with Add

Description

Adding a Node to the End of the Specified Node's Child Nodes with Add

Demo

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

class Program//from w w w . j  a  va2 s .c o  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")));
            
            Console.WriteLine(xDocument);
    }
}

Result

You can see we start with the base code and then add a Book element to the document's Books element.

Related Topic