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);
Console.WriteLine(xelFlight3.ToString());

// 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);
Console.WriteLine(separator);
Console.WriteLine(xdocFlight3.ToString());

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

*/

// 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”)
)
)
);

Console.WriteLine(separator);
Console.WriteLine(xelGOT.ToString());

string xmlOutputFilename = @”c:\XMLClass\IntroSamples\GameOfThrones.xml”;
//xelGOT.Save(xmlOutputFilename);

standardLinqQueries(xelGOT);
//XPathLinqQueries(xelGOT);
//queryExpressionsInLinq(xelGOT);

Console.WriteLine(separator);

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

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

Console.WriteLine(separator);
// 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”))
{
Console.WriteLine(
” Character=” + xelChar.Element(“Name”).Value +
” Actor=” + xelChar.Element(“Actor”).Value +
” Status=” + xelChar.Attribute(“Status”).Value
);
}
Console.WriteLine(“============ End: Loop through Characters ======\n\n”);

}

}
}

Filed under: XML/LINQ