1

I am using a Web Browser control to allow user to login to the server from a desktop(Windows form) app. after successful response, the server sends the response as XML that contains Security token but i can't read this xml from the WebBrowser control. the browser control converts the XML to strange HTML that i can't processed. Here is my Code:-

var webBrowser = new WebBrowser
        {
            Dock = DockStyle.Fill
        };
        webBrowser.DocumentCompleted += webBrowser_DocumentCompleted;
        webBrowser.Navigate(url);

void webBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {
        var browser = sender as WebBrowser;
        if (browser != null)
        {

            HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
            doc.LoadHtml(browser.DocumentText);
            var appNode = doc.DocumentNode.SelectNodes("//myTag");
        }
    }

and this is my XML that i received from the server

<MyCustomXml>
<securityToken>RETERET-SDSDSD-DSFSDFS-SDASDAS-ASDASDASDASD</securityToken>
<organizationUrl>http://myOrganization.Com/</organizationUrl>
</MyCustomXml>
yo2011
  • 971
  • 2
  • 12
  • 38
  • any help for this issue – yo2011 Apr 03 '16 at 13:54
  • You need to be more clear. What strange html server returns, what is expected xml, why use browser at all and not call server directly? – Evk Apr 04 '16 at 15:42
  • I use webbrowser to allow user to login and i get xml response from the server but when u read the webbrowser document html i get an html that i can't process – yo2011 Apr 04 '16 at 19:40
  • Sorry, didn't became any clearer :) What is exactly that html you "can't process"? Why you use browser and not ask user for password and then do HttpWebRequest to server directly? – Evk Apr 04 '16 at 19:43
  • We use webbrowser because we don't know the login mechanism for the user. Some user may have user name and password but other may have different security mechanism – yo2011 Apr 04 '16 at 19:46
  • Only the thing we know is the authentication url. The outbut is an html that contains a lot of styles and tds and my xml is injected as an attributes to the html. You can try this scenario with webbrowser that connects to a server that returns xml – yo2011 Apr 04 '16 at 19:50

1 Answers1

1

The WebBrowser control transforms the received XML into a HTML page to have a more user-friendly display. For example, if I visit http://www.w3schools.com/xml/note.xml with IE, I will see this interactive display :

screenshot1

If you look at the source of this display, you will see this document interpreted as HTML (note the add of HTML tags and inline CSS) :

screenshot2

This code is what you extract from your WebBrowser because it has interpreted it as HTML just like IE does, and it is his job as a WebBrowser to make document visible for the user. I don't think there is a way to disabled this feature.

So my thought is that you are trying to implement the authentification system the wrong way, it's a bad idea to display XML in the WebBrowser and trying to extract it. If you really want to use a WebBrowser system I would recommend to rather use a plain text response from your server (see the HTTP header Content-Type), this way the WebBrowser won't try to interpret it.

Then concerning the parsing, I recommend you to use XmlDocument api instead of the HtmlAgilityPack as you are working with a well formatted XML document.

Finally, I also suggest you to take a look at this thread which talks about the same problem as yours.

Community
  • 1
  • 1
bviale
  • 5,245
  • 3
  • 28
  • 48