0

With this code I get the HTLM document:

private static async Task<HtmlDocument> GetHtmlOfPage(string website)
{
           return await new HtmlWeb().LoadFromWebAsync(website);
}

The Xpath of the element is:

id('ContentHolderMain_ContentHolderMainContent_ContentHolderMainContent_pnlDaily')/x:div/x:ol/x:li[3]/x:ul

(using Xpath Checker for firefox).

The code for retrieving the values:

public List<string> GetTypesOfFood(string website)
{
        List<string> TypesOfFood = new List<string>();

        HtmlDocument document = GetHtmlOfPage(website).Result;
        /* HtmlNode docNodes = document.DocumentNode;
         HtmlNode ul = docNodes.Element("id('ContentHolderMain_ContentHolderMainContent_ContentHolderMainContent_pnlDaily')/x:div/x:ol/x:li[1]/x:ul[@title]");*/

        HtmlNode div = document.GetElementbyId("ContentHolderMain_ContentHolderMainContent_ContentHolderMainContent_pnlDaily");

        if(div != null)
        {
            var ul = div.Element("//div[@class='holderRestaurantInfo']/ol/li[1]/ul/@title").GetAttributeValue("title", "title");
            string a = ul;

            TypesOfFood.Add(a);
        }

        return TypesOfFood;
}

I expect values like "Meat", "Vegatarian",.... but I get no results. What I am doing wrong and how to use Xpath with UWP?

Update (21.6.2017):

I finally managed to get the values I was looking for:

public List<string> GetTypesOfFood()
    {
        if(!InternetConnection)
        {
            flag = -3;
            return null;
        }
        List<string> TypesOfFood = new List<string>();

        WebResponse response = GetResponse().Result;
        Stream stream = response.GetResponseStream();
        string result = "";
        using (StreamReader sr = new StreamReader(stream))
        {
            result = sr.ReadToEnd();
        }


        HtmlDocument MobileDocument = new HtmlDocument();
        MobileDocument.LoadHtml(result);

           var images = MobileDocument.DocumentNode.SelectNodes("//img[@class='pull-right']"); 
            foreach(var image in images)
            {
                HtmlAttribute title = image.Attributes[@"title"];
                TypesOfFood.Add(title.Value);
            }
     return TypesOfFood;
    }
   private async Task<WebResponse> GetResponse()
    {
        WebRequest req = WebRequest.Create("https://www.studentska-prehrana.si/sl/restaurant/Details/2521");
        WebResponse r = await req.GetResponseAsync();
            return r;
    }

Edit: It also seems my Firefox Xpath plugin is outdated due to the "x:" signs. No code uses them.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
shanji97
  • 71
  • 1
  • 7
  • 19

1 Answers1

1

What I am doing wrong and how to use Xpath with UWP?

According to your code snippet, it seems like the third party package you are trying to use is Html Agility Pack which is not fully supported in universal app currently. Please try to use HtmlAgilityPack for .NET Core which is supported in UWP app.

Using Xpath in UWP can be same as what you did in other places. For example, using SelectNodes method of HtmlAgilityPack for .NET Core with Xpath string above as follows:

WebRequest request = HttpWebRequest.Create("your uri");
WebResponse response = await request.GetResponseAsync();
Stream stream = response.GetResponseStream();
var result = "";
using (StreamReader sr = new StreamReader(stream))
{
    result = sr.ReadToEnd();
}
HtmlDocument MobileDocument = new HtmlDocument();
MobileDocument.LoadHtml(result);
var nodes = MobileDocument.DocumentNode.SelectNodes("//id('ContentHolderMain_ContentHolderMainContent_ContentHolderMainContent_pnlDaily')/x:div/x:ol/x:li[3]/x:ul]");
foreach (var item in nodes)
{ 
 ...
}

There is a similar thread here you may also reference. If you still have issues, provide the html content or the html Url to let us have testing on our side.

Sunteen Wu
  • 10,509
  • 1
  • 10
  • 21
  • Thanks for the solution. My code was working one day and then the whole site got renewed, there is now a new html formating, but anyway thanks for the tip. The site is https://www.studentska-prehrana.si/sl/restaurant/Details/2521# and the Xpath is: id('menu-list')/x:div[2]/x:div/x:div/x:div[2]/x:i/x:img The code for the text fetch I'll write myself. – shanji97 Jun 20 '17 at 16:45