Assembly error using HtmlAgilityPack

c# html-agility-pack reference windows-phone-8.1 xpath

Question

I want to create an app that takes something from a web page and that shows it to the user. In order to do this thing, i used HtmlAgilityPack. I added the reference to the project (v. 1.4.6.0 from NuGet) and i used the code that a user posted in another question some years ago.

            HtmlAgilityPack.HtmlDocument htmlDoc = new HtmlAgilityPack.HtmlDocument();
            // There are various options, set as needed
            htmlDoc.OptionFixNestedTags = true;

            // filePath is a path to a file containing the html
            htmlDoc.Load(filePath);
            if (htmlDoc.ParseErrors != null && htmlDoc.ParseErrors.Count() > 0)
            {
                // Handle any parse errors as required
            }
            else
            {
                if (htmlDoc.DocumentNode != null)
                {
                    HtmlAgilityPack.HtmlNode bodyNode = htmlDoc.DocumentNode.SelectSingleNode("//body");

                    if (bodyNode != null)
                    {
                        // Do something with bodyNode
                    }
                }
            }

My problem is that i get the following error:

The type 'System.Xml.XPath.IXPathNavigable' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Xml.XPath, Version=2.0.5.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.

So i tried to add the reference using the following dll:

c:\Program Files (x86)\Microsoft SDKs\Silverlight\v5.0\Libraries\Client\System.Xml.XPath.dll

But this time i get the following error (i translated it because i get an italian error, so it may not be the correct translation):

It is not possible to add a reference to an assembly to the project that is not compatible or of an above version

What can i do?

Popular Answer

It seems that HtmlAgilityPack (HAP) for windows phone 8.1 store apps doesn't support XPath, similar to HAP for Windows 8 store apps.

In this case, you can use LINQ API of HAP instead of XPath API, for example :

......
if (htmlDoc.DocumentNode != null)
{
    HtmlAgilityPack.HtmlNode bodyNode = doc.DocumentNode
                                           .DescendantsAndSelf("body")
                                           .FirstOrDefault();
    if (bodyNode != null)
    {
        // Do something with bodyNode
    }
}
......

Even if you actually have windows phone silverlight apps project, LINQ API is still a possible workaround.




Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why