The following relates to this XML from yesterday’s blog. You can get the surrounding code in that blog.
It’s advisable to use the LINQ style of query when possible, but there might be that day where you have a long complicated XPath, and you just want to use with an XElement that you loaded from LINQ. This is how to do that, with several examples.
This is a sample from Complete Guide to XML for Microsoft Developers (Udemy Course).
// 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") ) ) );
public static void XPathLinqQueries(XElement xelGOT) { // // NOTE: The above is the preferred methodology using LINQ. // In theory, you would use LINQ queries instead of XPATH // queries, but there are two XPATH methods we will demonstrate. // //Second Actor using XPath string xpathCharacter2A = "./Characters/Character[2]/Actor"; var secondActorViaXPathA = xelGOT.XPathSelectElement(xpathCharacter2A).Value; Console.WriteLine("Second Actor via XPathA=" + secondActorViaXPathA); // NOTE: XPathSelectElement only returns XElements, // so you cannot do count(//Characters) or /Actor/text(). // Instead, you have to use the more general XPathEvaluate. // But you must also wrap the xpath with like this: // string(varXPath) // The XPathEvaluate method returns an object that can contain // a bool, a double, a string, or an or IEnumerable. string xpathCharacter2B = "./Characters/Character[2]/Actor/text()"; string secondActorViaXpathEval = xelGOT.XPathEvaluate("string(" + xpathCharacter2B + ")") as string; Console.WriteLine("Second Actor via XPathB=" + secondActorViaXpathEval); string xpathCharacterCount = "count(./Characters/Character)"; string strCharacterCount = xelGOT.XPathEvaluate("string(" + xpathCharacterCount + ")") as string; Console.WriteLine("CharacterCount=" + strCharacterCount); double? nullableCharacterCount = xelGOT.XPathEvaluate(xpathCharacterCount) as double?; Console.WriteLine("Numeric CharacterCount=" + nullableCharacterCount); string xpathCharacter2C = "./Characters/Character"; IEnumerable results = (IEnumerable)xelGOT.XPathEvaluate(xpathCharacter2C); //Loop through Characters Console.WriteLine("\n\n============ Start: XPath Loop through Characters ======"); foreach (var result in results.Cast()) { Console.WriteLine( " Character=" + result.Element("Name").Value + " Actor=" + result.Element("Actor").Value + " Status=" + result.Attribute("Status") ); } Console.WriteLine("============ End: Loop through Characters ======\n\n"); }