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());
    }
}

   
     


Transforming an XML Document

   
 

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", "A"),
              new XElement("LastName", "B")),
            new XElement("Book",
              new XAttribute("type", "Author"),
              new XElement("FirstName", "C"),
              new XElement("LastName", "D"))));

        Console.WriteLine("Here is the original XML document:");
        Console.WriteLine("{0}{1}{1}", xDocument, System.Environment.NewLine);
    }
}

    


XDocument transformation demo

   
 
using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using System.Xml;
using System.Xml.Xsl;
using System.IO;
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", "A"),
              new XElement("LastName", "B")),
              new XElement("Book",
            new XAttribute("type", "Author"),
              new XElement("FirstName", "C"),
              new XElement("LastName", "D"))));

        XDocument transformedDoc = new XDocument();
        using (XmlWriter writer = transformedDoc.CreateWriter()) {
            XslCompiledTransform transform = new XslCompiledTransform();
            transform.Load(XmlReader.Create(new StringReader("c:your.xls")));
            transform.Transform(xDocument.CreateReader(), writer);
        }
        Console.WriteLine(transformedDoc);
    }
}

    


Use XDeclaration,XElement, XAttribute to create an XDocument

   
 
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 = new XDocument(new XDeclaration("1.0", "UTF-8", "yes"),
                                        new XElement("people",
                                            new XElement("idperson",
                                            new XAttribute("id", 1),
                                            new XAttribute("year", 2004),
                                            new XAttribute("salary", "100"))));

        System.IO.StringWriter sw = new System.IO.StringWriter();
        xml.Save(sw);
        Console.WriteLine(sw);
    }
}

    


Creating a Declaration and Setting the Document's Declaration Property to It

   
 

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("BookParticipant"));
        XDeclaration xDeclaration = new XDeclaration("1.0", "UTF-8", "yes"); xDocument.Declaration = xDeclaration;
        Console.WriteLine(xDocument);
    }
}

    


Creating a Declaration

   
 

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 XElement("BookParticipant"));

        Console.WriteLine(xDocument);
    }
}