using System; using System.Linq; using System.Collections; using System.Collections.Generic; using System.Xml; using System.Xml.Linq; public class MainClass { public static void Main() { XDocument xDocument = new XDocument( new XElement("Books", new XElement("Book", new XAttribute("type", "Author"), new XAttribute("experience", "first-time"), new XAttribute("language", "English"), new XElement("FirstName", "J"), new XElement("LastName", "R")))); xDocument.Save("book.xml"); } }
XML LINQ
Intentionally Exposing the Halloween Problem
using System; using System.Linq; using System.Collections; using System.Collections.Generic; using System.Xml; using System.Xml.Linq; public class MainClass { public static void Main() { XDocument xDocument = new XDocument( new XElement("Books", new XElement("Book", new XAttribute("type", "Author"), new XElement("FirstName", "J"), new XElement("LastName", "R")), new XElement("Book", new XAttribute("type", "Author"), new XElement("FirstName", "E"), new XElement("LastName", "B")))); 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) { Console.WriteLine("Removing {0}= {1} ¡", element.Name, element.Value); element.Remove(); } Console.WriteLine(xDocument); } }
Creating an XML Document with XDocument
using System; using System.Linq; using System.Collections; using System.Collections.Generic; using System.Xml; using System.Xml.Linq; public class MainClass { public static void Main() { XDocument xDocument = new XDocument( new XDeclaration("1.0", "UTF-8", "yes"), new XDocumentType("Books", null, "Books.dtd", null), new XProcessingInstruction("Book", "out-of-print"), new XElement("Books")); Console.WriteLine(xDocument); } }
Using the LINQ to XML API to Create an XML Document and Adding Some Structure
using System; using System.Linq; using System.Collections; using System.Collections.Generic; using System.Xml; using System.Xml.Linq; public class MainClass { public static void Main() { XDocument xDocument = new XDocument( new XElement("Books", new XElement("Book", new XAttribute("type", "Author"), new XElement("FirstName", "J"), new XElement("LastName", "R")))); Console.WriteLine(xDocument.ToString()); } }
Get Encoding, Version and standalone from XML declaration
using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data.Linq; using System.Reflection; using System.Xml.Linq; class Program { static void Main(string[] args) { XDocument xml = XDocument.Load("Hello_XLINQ.xml"); Console.WriteLine("Encoding: {0}", xml.Declaration.Encoding); Console.WriteLine("Version: {0}", xml.Declaration.Version); Console.WriteLine("Standalone: {0}", xml.Declaration.Standalone); } }
Query an XML document with Linq
using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data.Linq; using System.Reflection; using System.Xml.Linq; class Program { static void Main(string[] args) { XDocument xml = XDocument.Load(@"Employee.xml"); var query = from p in xml.Elements("people").Elements("person") where (int)p.Element("id") == 1 select p; foreach (var record in query) { Console.WriteLine("Employee: {0} {1}", record.Element("firstname").Value, record.Element("lastname").Value); } } }
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()); } }