Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

browser control and xml 1

Status
Not open for further replies.

SgtPeppa

Programmer
Jul 7, 2003
154
DE
Hey, all,

I am really bad in these UI things, so I hope some of you can give me a hint to accomplish this:

I have a browser control placed on my WinForms Application. I also have a string containing a xml document.

I want to display the xml string inside my browser control. How can I achieve this?

Thanks in advance,

Stephan

 
Use the Navigate method to navigate to your .xml file (file:///c:\somedir\myfile.xml)

Chip H.


If you want to get the best response to a question, please check out FAQ222-2244 first
 
I do not have a file, all I have is a string containing an xml. And writing the file is not an option since the functionality is on a UserControl which is supposed to be used on Web Applications as well as Win-Aapplications.

Any other ideas?
 
Sorry, no.

Chip H.


If you want to get the best response to a question, please check out FAQ222-2244 first
 
Can you access the Browser Control's DOM? And set the contents via the InnerHTML or InnerText (not technically W3C DOM properties, but will work in MS/IE) to your XML string?



Thomas D. Greer
Providing PostScript & PDF
Training, Development & Consulting
 
Not that I know of, the Document Property of the Browser Control is read only.

Do you have an example?

Stephan
 
I think my approach will work. You just need to dig a little deeper. The "document" property returns an object. You have to cast that object to the HTML "dom". That gives you something with which you can interact.

You've already found/added the Web Browser Control. Now you need to add the Microsoft HTML Object Library (MSHTML) to your project. Remeber to add "Using mshtml;" to your code.

Here's an example using text. It should work the same way for any string, including XML fragments:

Code:
//first navigate to create a usable Document object.
string str = "";
string url = "about:blank";
System.Object nullObject = 0;
System.Object nullObjStr = str;
axWebBrowser1.Navigate(url,ref nullObject, ref nullObjStr, ref nullObjStr, ref nullObjStr);

//now return that object
//and cast the result to an HTML object
//and use the .write() method		
string myXML = "This is a test";
object [] content = {myXML};
System.Object docObj = axWebBrowser1.Document;
IHTMLDocument2 hDoc = (mshtml.IHTMLDocument2) docObj;
hDoc.write(content);



Thomas D. Greer
Providing PostScript & PDF
Training, Development & Consulting
 
I've been playing with this a little more. It doesn't do exactly what I expected.

One thing you should add is a loop to check to see that the browser control is finished loading the inital (blank) document.

Check the ReadyState flag:

Code:
while(axWebBrowser1.ReadyState != SHDocVw.tagREADYSTATE.READYSTATE_COMPLETE)
{
  Application.DoEvents();
}

The main problem is that the xml isn't rendered as XML, and after much Google searching, I've concluded that that is "just the way it is". So if you were expecting that nice collabsible-tree view of your XML, you can't.

The only work-around I can think of is to write your XML to a temporary file, and navigate to it.



Thomas D. Greer
Providing PostScript & PDF
Training, Development & Consulting
 
Ok there is even an event that occurs once the document is loaded.
Code:
		private void brwContent_NavigateComplete2(object sender, AxSHDocVw.DWebBrowserEvents2_NavigateComplete2Event e)
		{
			System.Object docObj = brwContent.Document;
			IHTMLDocument2 hDoc = (mshtml.IHTMLDocument2) docObj;
			string myContent =Hello World>;
			hDoc.write(myContent);
		}

This works thanks a lot for all your work, I am looking into displaying xml now.

Thanks again I was so lost, I really appreciated all your effort and work you put into this to help me. Thanks again.

Stephan
 
No problem. Thanks for the "star". Just another note for the sake of completeness, the "Document" property of the web browser returns a generic object. If it's HTML, you can cast it to the IHTMLDocument2 object.

But the Document may not be an HTML document. It could be, for example, a movie or PDF file or whatever. If you have .NET libraries for handling those various formats, you can cast to that specific document type.



Thomas D. Greer
Providing PostScript & PDF
Training, Development & Consulting
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top