user1528573
user1528573

Reputation: 95

Read content of Web Browser in WPF

Hello Developers I want to read external content from Website such as element between tag . I am using Web Browser Control and here is my code however this Code just fills my Web browser control with the Web Page

public MainWindow()
{
    InitializeComponent();

    wbMain.Navigate(new Uri("http://www.annonymous.com", UriKind.RelativeOrAbsolute));
}

Upvotes: 4

Views: 9966

Answers (3)

Stefan Cvetanovski
Stefan Cvetanovski

Reputation: 298

As I understood from your question, you are only trying to parse the HTML data, and you don't need to show the actual web page. If that is the case than you can take a very simple approach and use HttpWebRequest:

    var _plainText = string.Empty;
    var _request = (HttpWebRequest)WebRequest.Create("http://www.google.com");
    _request.Timeout = 5000;
    _request.Method = "GET";
    _request.ContentType = "text/plain";
    using (var _webResponse = (HttpWebResponse)_request.GetResponse())
    {
        var _webResponseStatus = _webResponse.StatusCode;
        var _stream = _webResponse.GetResponseStream();
        using (var _streamReader = new StreamReader(_stream))
        {
            _plainText = _streamReader.ReadToEnd();
        }
    }

Upvotes: 4

Tomislav Markovski
Tomislav Markovski

Reputation: 12346

You can use the Html Agility Pack library to parse any HTML formatted data.

HtmlDocument doc = new HtmlDocument();
doc.Load(wbMain.DocumentText);

var nodes = doc.SelectNodes("//a[@href"]);

NOTE: The method SelectNode accepts XPath, not CSS or jQuery selectors.

var node = doc.SelectNodes("id('my_element_id')");

Upvotes: 4

Darajan
Darajan

Reputation: 883

Try this:

dynamic doc = wbMain.Document;
var htmlText = doc.documentElement.InnerHtml;

edit: Taken from here.

Upvotes: 2

Related Questions