XPath Document Evaluate Method C#

XPath can be used to find specific nodes that match some criteria defined in the XPath expression. You can use XPath query to navigate through an XML or HTML document. Accordingly, this article explains how to use XPath to select specific nodes as per your requirements.

Work with XPath to Select Nodes from XML or HTML Files - C# API Installation

Aspose.HTML for .NET API can be used to automate the process to select specific type of nodes, or read specific information from a selected node. You can quickly configure the API by downloading the DLL file from the New Releases page or using the following NuGet installation command:

PM> Install-Package Aspose.Html

Use XPath Document.Evaluate() Method to Get Images from an HTML Webpage in C#

You may use the Document.Evaluate() method to get images from an HTML webpage. Please follow the steps below to complete this process:

  1. Initialize an object of HTMLDocument class to load the source file.
  2. Pass the XPath expression and other parameters to the Evaluate method.
  3. Iterate through the selected nodes and get the link for each image.

The following code sample demonstrates how to use XPath Document.Evaluate() method to get images from an HTML webpage:

Use XPath Document.Evaluate() Method to Select Nodes from XML File in C#

In certain cases, you might need to select specific nodes from an XML file. The following steps explain how to meet such requirements:

  1. Load the source XML file with the HTMLDocument class.
  2. Call the Evaluate method while passing the XPath expression and other parameters.
  3. Iterate over the selected nodes and print the entire content of selected nodes.

The code snippet below shows how to use XPath Document.Evaluate method to select nodes from XML file:

Use XPath Document.Evaluate() Method to Pick Specific Information from Selected Nodes in C#

Sometimes you may need to get particular details from a Node instead of entire contents. You can follow the steps below to use XPath query for getting specific information from the selected nodes.

  1. Access the input XML file using the HTMLDocument class.
  2. Invoke the Evaluate method to get the nodes matching the XPath expression.
  3. Loop through the matching nodes and print the specific content from each node.

The following code sample explains how to pick specific information from a selected node in XML using the XPath Document.Evaluate method:

Use XPath to Select Nodes in C# - Read More

You may learn further details about working with XPath Query by visiting the following topics in the documentation section.

XPath Query to Select Nodes from HTML or XML in C# - Free Evaluation License

You may test the API features to their full capacity, without any limitations by requesting a free temporary license.

Conclusion

In this article, you have learned how to use XPath Query Document.Evaluate method to select specific nodes or particular contents of the selected nodes that match the XPath expression. Whereas, in case you want to discuss any of your concerns, please reach out to us at free support forum.

See Also

Use CSS Selectors in HTML using C#