Calling the Second AncestorsAndSelf Prototype

image_pdfimage_print
   
 

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

    


Get Attribute with namespace

image_pdfimage_print
   
 

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

    }
}

    


Accessing an Element's Attributes Using the Attributes Method

image_pdfimage_print
   
 

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

    


Calling the Second Ancestors Prototype

image_pdfimage_print
   
 

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.Ancestors("Book")) {
            Console.WriteLine("Ancestor element: {0}", element.Name);
        }
    }
}

    


Traversing Up from an XElement Object via the AncestorsAndSelf Method

image_pdfimage_print
   
 

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("a new author."),
         new XProcessingInstruction("AuthorHandler", "new"),
         new XAttribute("type", "Author"),
         new XElement("FirstName",
       new XText("A"),
       new XElement("NickName", "J")),
      new XElement("LastName", "R")),
      new XElement("Book",
      new XAttribute("type", "Author"),
      new XElement("FirstName", "E"),
      new XElement("LastName", "B"))));
        foreach (XElement element in firstParticipant.
        Element("FirstName").Element("NickName").AncestorsAndSelf()) {
            Console.WriteLine(element.Name);
        }
    }
}

    


Calling the First AncestorsAndSelf Prototype

image_pdfimage_print
   
 

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()) {
            Console.WriteLine("Ancestor element: {0}", element.Name);
        }
    }
}

    


An Example of Calling the First Ancestors Prototype

image_pdfimage_print
   
 

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 xDocument = new XElement(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.Ancestors()) {
            Console.WriteLine("Ancestor element: {0}", element.Name);
        }
    }
}