LINQ XML Navigation Example

Example of how to create XML in memory using LINQ, then how to retrieve elements and attributes from it in the LINQ style.
In a future blog, I will show you how to use XPath, and then LINK Query Expressions.

This is a sample from Complete Guide to XML for Microsoft Developers (Udemy Course).

using System;
//using System.Collections;
using System.Collections; //cannot use this one, as it causes clash with IEnumerable
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Linq; // for XElement
using System.Xml.XPath; // in order to use XPathSelectElement(s)

namespace LINQ_Practice
class Program
public static string separator = “\n\n——————————————–\n”;
static void Main(string[] args)

//LINQ – Language Integrated Query

string xmlInFilename = @”c:\XMLClass\IntroSamples\Flight03.xml”;
// NOTE: we don’t have to instantiate XElement
XElement xelFlight3 = XElement.Load(xmlInFilename);

// Per MSDN article:
// Only use XDocument when you need to add a comment
// or processing instruction at the root level.
XDocument xdocFlight3 = XDocument.Load(xmlInFilename);

XElement xelCourse = XElement.Parse(“Complete Guide to XML for Microsoft DevelopersNeal Walters“);


// GOT = Game of Thrones (data)
XElement xelGOT =
new XElement(“GameOfThrones”,
new XElement(“Characters”,
new XElement(“Character”,
new XAttribute(“Status”, “deceased”),
new XElement(“Name”, “Daenerys Targaryen”),
new XElement(“Actor”, “Emilia Clarke”)
new XElement(“Character”,
new XAttribute(“Status”, “living”),
new XElement(“Name”, “Jon Snow”),
new XElement(“Actor”, “Kit Harrington”)
new XElement(“Character”,
new XAttribute(“Status”, “living”),
new XElement(“Name”, “Tyrion Lannister”),
new XElement(“Actor”, “Peter Dinklage”)
new XElement(“Cities”,
new XElement(“City”,
new XElement(“Name”, “King’s Landing”),
new XElement(“Location”, “The Crownlands, Westeros”)
new XElement(“City”,
new XElement(“Name”, “Braavos”),
new XElement(“Location”, “The Free Cities, Essos”)
new XElement(“City”,
new XElement(“Name”, “Lannisport”),
new XElement(“Location”, “The Westerlands, The Seven Kingdoms”)


string xmlOutputFilename = @”c:\XMLClass\IntroSamples\GameOfThrones.xml”;



Console.WriteLine(“\n\n Press enter to end ….”);

public static void standardLinqQueries(XElement xelGOT)
// Our XElement is “GameOfThrones” so the
// navigation and XPATH below must start from there

// NOTE: Don’t put root element in quotes…
string firstActor = xelGOT.Elements(“Characters”).First().Elements(“Character”).First().Elements(“Actor”).First().Value;
Console.WriteLine(“First Actor=” + firstActor);

//illustrate ElementAt method – zero-based
string secondActor = xelGOT.Elements(“Characters”).Elements(“Character”).ElementAt(1).Elements(“Actor”).First().Value;
Console.WriteLine(“Second Actor=” + secondActor);

//illustrate Skip method
string thirdActor = xelGOT.Elements(“Characters”).Elements(“Character”).Skip(2).Elements(“Actor”).First().Value;
Console.WriteLine(“Third Actor=” + thirdActor);

//Loop through Characters
Console.WriteLine(“\n\n============ Start: Loop through Characters ======”);
foreach (XElement xelChar in xelGOT.Elements(“Characters”).Elements(“Character”))
” Character=” + xelChar.Element(“Name”).Value +
” Actor=” + xelChar.Element(“Actor”).Value +
” Status=” + xelChar.Attribute(“Status”).Value
Console.WriteLine(“============ End: Loop through Characters ======\n\n”);



Leave a Reply

Recent Posts




All Rights Reserved Theme by 404 THEME.