You can find more details at: Use XPath to Select Nodes using C#
Last active
January 31, 2023 13:10
-
-
Save aspose-com-gists/a651425e350e16b9213ed6c3873bcddf to your computer and use it in GitHub Desktop.
Use XPath to Select Nodes in C# | Document.Evaluate() Method Examples for XML or HTML File
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Create an instance of a document | |
using (var doc = new Aspose.Html.HTMLDocument(Path.Combine(dataDir, "cars.xml"))) | |
{ | |
// Evaluate the XPath expression | |
var dealers = doc.Evaluate("//Dealer[descendant::Car[descendant::Model > 2005 and descendant::Price < 25000]]", doc, doc.CreateNSResolver(doc), XPathResultType.Any, null); | |
Aspose.Html.Dom.Node dealer; | |
// Iterate over the resulted nodes and print their contents to the console | |
while ((dealer = dealers.IterateNext()) != null) | |
{ | |
Console.WriteLine(dealer.TextContent); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Create an instance of a document | |
using (var doc = new Aspose.Html.HTMLDocument(Path.Combine(dataDir, "cars.xml"))) | |
{ | |
// Select dealers that match XPath expression | |
var dealers = doc.Evaluate("//Dealer[descendant::Car[descendant::Model > 2005 and descendant::Price < 25000]]", doc, doc.CreateNSResolver(doc), XPathResultType.Any, null); | |
Aspose.Html.Dom.Node dealer; | |
// Iterate over the selected dealers | |
while ((dealer = dealers.IterateNext()) != null) | |
{ | |
// Get and print Dealer name and Telephone | |
var dealerInfo = doc.Evaluate("concat('Dealer name: ', Name, ' Telephone: ', Telephone)", dealer, doc.CreateNSResolver(doc), XPathResultType.String, null); | |
Console.WriteLine(dealerInfo.StringValue); | |
// Select and print CarID that match XPath expression | |
var carIds = doc.Evaluate(".//Car[descendant::Model > 2005 and descendant::Price < 25000]/@CarID", dealer, doc.CreateNSResolver(doc), XPathResultType.Any, null); | |
Aspose.Html.Dom.Node carId; | |
while ((carId = carIds.IterateNext()) != null) | |
{ | |
Console.WriteLine("Car id: " + carId.TextContent); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Create an instance of an HTML document | |
using (var doc = new Aspose.Html.HTMLDocument(Path.Combine(dataDir, "xpath.htm"))) | |
{ | |
// Evaluate the XPath expression | |
var result = doc.Evaluate("//main/div[position() mod 2 = 1]//img[@class = 'photo']", doc, doc.CreateNSResolver(doc), Aspose.Html.Dom.XPath.XPathResultType.Any, null); | |
// Iterate over the resulted nodes and print them to the console | |
Aspose.Html.Dom.Node node; | |
while ((node = result.IterateNext()) != null) | |
{ | |
var img = (Aspose.Html.HTMLImageElement)node; | |
Console.WriteLine(img.Src); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment