user2617500
user2617500

Reputation: 1

WPF WebBrowser - built-in search dialog doesn't work if content was loaded with NavigateToString

I use WebBrowser to display generated XML. My XML string loaded into browser by call to NavigateToString:

var text = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" 
         + Environment.NewLine
         + "<whatever/>";
Browser.NavigateToString(text);

After browser loads string content I'm trying to search for any displayed text using standard Ctrl+F search dialog - but it always shows warning "No matches found".

If I save the XML string to a file and use Browser.Navigate(filename) it works.

Any ideas?

Upvotes: 0

Views: 1050

Answers (2)

florian
florian

Reputation: 1

I just had the same problem. There is even the possibility to open the xml file directly using the overload:

webbrowser.Navigate(string filepathToXML)

Going this way, the builtin search panel works like a charm.

Upvotes: 0

noseratio
noseratio

Reputation: 61744

When you navigate to a file, the WebBrowser control performs MIME-type sniffing (often using the file extension as a hint). Then it creates an Active Document object of the corresponding type. Most often it's an instance of MSHTML Document, but can also be an XML, PDF or Word document, all of which support Active Document interfaces.

Now, when you navigate to a string with NavigateToString, the WebBrowser doesn't make any attempts to recognize the document type, and simply creates and instance of MSHTML Document (rather than XML Document), then tries to parse the content as HTML and fails.

I don't think you can get the desired behavior using NavigateToString, and I believe the same applies to NavigateToStream. To illustrate what's going on, take your XML content and save it as filename.html, filename.txt and filename.xml. Try opening each file with IE.

On a side note, when you navigate to a URL, the server actually has an option to suggest the MIME type, using HTTP headers. The browser may or may not tolerate such suggestion (it will still perform some validation checks).

The bottom line: you will not be able to render XML with NavigateToString or NavigateToStream. You're going to have to convert it to HTML first (e.g., with an XSLT transform).

Upvotes: 0

Related Questions