Accessing the XML Document from an XElement Object via the Document Property

   
 

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 firstParticipant;
        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", firstParticipant =
          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"))));

        Console.WriteLine(firstParticipant.Document);
    }
}

    


Is an element empty

   
 
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) {
        XElement xml = XElement.Load("Employee.xml");
        XElement firstName = xml.Descendants("firstname").First();
        Console.WriteLine("Is FirstName tag empty? {0}", firstName.IsEmpty ? "Yes" : "No");
        XElement idEmployee = xml.Descendants("idperson").First();
        Console.WriteLine("Is idperson tag empty? {0}", idEmployee.IsEmpty ? "Yes" : "No");
    }
}

    


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