Accessing the Previous Attribute with the PreviousAttribute 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 XElement("Books", firstParticipant =
            new XElement("Book",
              new XAttribute("type", "Author"),
              new XAttribute("experience", "first-time"),
              new XAttribute("language", "English"),
              new XElement("FirstName", "J"),
              new XElement("LastName", "R"))));
        Console.WriteLine(firstParticipant.FirstAttribute.NextAttribute.PreviousAttribute);
    }
}

    


Accessing the Next Attribute with the NextAttribute 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 XElement("Books", firstParticipant =
            new XElement("Book",
              new XAttribute("type", "Author"),
              new XAttribute("experience", "first-time"),
              new XAttribute("language", "English"),
              new XElement("FirstName", "J"),
              new XElement("LastName", "R"))));
        Console.WriteLine(firstParticipant.FirstAttribute.NextAttribute);
    }
}

    


Accessing an Element's First Attribute with the FirstAttribute 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 XElement("Books", firstParticipant =
            new XElement("Book",
              new XAttribute("type", "Author"),
              new XAttribute("experience", "first-time"),
              new XAttribute("language", "English"),
              new XElement("FirstName", "J"),
              new XElement("LastName", "R"))));
        Console.WriteLine(firstParticipant.FirstAttribute);
    }
}

    


Calling the Remove

   
 

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

        IEnumerable<XAttribute> attributes = xDocument.Element("Books").Elements("Book").Attributes();
        foreach (XAttribute attribute in attributes) {
            Console.WriteLine("Source attribute: {0} : value = {1}", attribute.Name, attribute.Value);
        }
        attributes.Remove();
        Console.WriteLine(xDocument);
    }
}

    


Accessing an Element's Attributes Using the Attributes Method

   
 

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 XComment("This is a new author."),
           new XProcessingInstruction("AuthorHandler", "new"),
         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"))));
        foreach (XAttribute attr in firstParticipant.Attributes()) {
            Console.WriteLine(attr);
        }
    }
}

    


Get Attribute with namespace

   
 

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("Hello_XLINQ.xml");
        XNamespace w = "http://schemas.microsoft.com/office/word/2003/wordml";

        XElement defaultFonts = xml.Descendants(w + "defaultFonts").First();
        Console.WriteLine("Default Fonts: {0}", (string)defaultFonts.Attribute(w + "ascii"));

    }
}

    


Calling the Second AncestorsAndSelf Prototype

   
 

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").Descendants("FirstName");
        foreach (XElement element in elements) {
            Console.WriteLine("Source element: {0} : value = {1}",element.Name, element.Value);
        }
        foreach (XElement element in elements.AncestorsAndSelf("Book")) {
            Console.WriteLine("Ancestor element: {0}", element.Name);
        }
    }
}