Use XElement.Parse to parse an element document

   
 
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) {
        string doc = @"<people>
                          <!-- Employee section -->
                          <person>
                            <id>1</id>
                            <firstname>C</firstname>
                            <lastname>L</lastname>
                            <idrole>1</idrole>
                          </person>
                           </people>";
        XElement xml = XElement.Parse(doc);
        Console.WriteLine(xml);
    }
}

    


Different Node Value Types Retrieved via Casting to the Node Value's Type

   
 

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() {
        XElement count = new XElement("Count", 12);
        Console.WriteLine(count);
        Console.WriteLine((int)count);

        XElement smoker = new XElement("Smoker", false);
        Console.WriteLine(smoker);
        Console.WriteLine((bool)smoker);

        XElement pi = new XElement("Pi", 3.1415926535);
        Console.WriteLine(pi);
        Console.WriteLine((double)pi);
    }
}

    


Immediate Execution of the XML Tree Construction

   
 

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() {

        string[] names = { "J", "P", "G", "P" };

        XElement xNames = new XElement("Beatles",
               from n in names
               select new XElement("Name", n));
        names[3] = "R";
        Console.WriteLine(xNames);

    }
}