windows-phone-7xamlwindows-phone-7.1

How to parse HTML page data in Windows Phone 7?


I want to do the below two tasks In Windows Phone 7 application.

1.Navigate to a web page (e.g.http://www.FlightsInd.com) and get the HTML page data.I wnat to ensure that all the Document data is completely downloaded.

In C#.Net i am doing this using below code:

WebBrowser objWB = new WebBrowser();
webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);
objWB.Navigate("http://www.FlightsInd.com")

here once the DocumentCompleted event is fired it means all the data in that request is downloaded.

2.Parse HTML page elements data.

In C#.Net i am doing this using below code.

       doc = webBrowser1.Document;
       btnElem = doc.GetElementById(streleid);

Can anyody help me with the equivalent classes/code for the above two implementations ?


Solution

  • Use WebBrowser Windows Phone control

    To Navigate to your page

    browser.Navigate(new Uri("http://www.FlightsInd.com"));

    To understand that navigation completed and content is loaded

    WebBrowser.Navigated Event

    WebBrowser.LoadCompleted Event - Occurs after the WebBrowser control has loaded content.

    WebBrowser.NavigationFailed Event - to track navigation failures

    The WebBrowser class events are raised in the following order: Navigating, Navigated, and LoadCompleted.

    To get Html source

    WebBrowser Windows Phone control contains special function to save the source for the HTML content currently displayed in WebBrowser control as a string:

    string html = browser.SaveToString();

    To parse Html

    Look at HTML Agility Pack

    What is the best way to parse html in C#?

    Parsing HTML String

    PS. Alternatively you can use webBrowser.InvokeScript (C#) with combination of js eval to invoke any js command which can use window.external.notify inside it to pass results back to C#.