CSharp - XML Data change between accessing

Description

XML Data change between accessing

Demo

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

class Program//from  ww w .j a  v a2  s  .  c  om
{
    static void Main(string[] args){
            XDocument xDocument = new XDocument(
          new XElement("Books",
            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"))));
        
        IEnumerable<XElement> elements =
          xDocument.Element("Books").Elements("Book");
        
        foreach (XElement element in elements)
        {
          Console.WriteLine("Source element: {0} : value = {1}",
            element.Name, element.Value);
        }
        
        foreach (XElement element in elements.ToArray())
        {
          Console.WriteLine("Removing {0} = {1} ...", element.Name, element.Value);
          element.Remove();
        }
        
        Console.WriteLine(xDocument);
    }
}

Result

Related Topic