Mega Code Archive

 
Categories / C# / XML LINQ
 

Create XDocument from XmlReader

using System; using System.Collections; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Data.Linq; using System.Reflection; using System.Xml; using System.Xml.Linq; class Program {     static void Main(string[] args) {         XmlReader reader = XmlReader.Create("Employee.xml");         XDocument xml = XDocument.Load(reader);         Console.WriteLine(xml);         XElement idperson = xml.Descendants("idP").Last();         idperson.Add(new XElement("idperson",                         new XAttribute("id", 1),                         new XAttribute("year", 2006),                         new XAttribute("salary", "16")));         StringWriter sw = new StringWriter();         XmlWriter w = XmlWriter.Create(sw);         xml.Save(w);         w.Close();         Console.WriteLine(sw.ToString());     } }