Hey Guys

I'm kind of a newbie to C#. Now the syntax and the like don't bother me because I love java, so the actual programming is no problem. However, I'm trying to do something I can't figure out. I want to write a module in Visual C# (Visual Studio 2008) to read from an XML file and format it into HTML. Now I think I've got this previous part down, that shouldn't give me any problems. However, what I need to do now is to display this HTML in my form. In other words, I need a component which is an HTML Interpreter, or some other method to do this. Can anyone help me out on this?

Thanks in advance,
Mark.

Recommended Answers

All 7 Replies

You can use the WebBroswer control to display text. Drag a WebBrowser control onto your form. Put this in your Form_Load():

webBrowser1.Navigate("about:blank");

Then, use this code to fill it:

bool bError = true;
while (bError)
{
    try
    {
        webBrowser1.Document.Body.InnerHtml = "<HTML><BODY><H1>Hi!</H1</BODY></HTML>";
        bError = false;
    }
    catch (Exception exp)
    {
        ;
    }
}

The navigate to "about:blank" needs some time to complete; the loop and try..catch will help if the navigation is not complete. If you try too soon to put text into the control, you may get a NullReferenceException, since the "Body" member will still be NULL.

Hope this helps!

Ah thank you so much

Don't know where I'd be without the helpful forum folk :D

Thanks again

Hey

Sorry to be a bother again (I have a bad habit of doing this) but I'm having some serious trouble with this). Using the exact code you've supplied, My programme simply hangs forever. If I comment out the line setting the InnerHtml, the programme runs, but of course doesn't display what I want it to. Any chance anybody could help me with this?

Thanks in advance
Mark

In my sample, I put the loading of "about:blank" in Form_Load() to give the control time to navigate (and more important), initialize the "Body" member of the WebControl. If you put the "webcontrol1.Navigate" in the same routine that you try to load the control with your content, you will hang (I was seeing the same thing). Another sample I saw on the Internet (it was in VB.NET, though), was performing "DoEvents" to yield control back to the main loop to allow the webcontrol to intialize. I tried the same thing with a Thread.Sleep(200) call, but it didn't help.

All I can say is try to separate the "about:blank" call into a separate routine (perhaps even the Form's constructor) to give it time to load and initialize the "Body" element, so in a later routine, you can supply your content to the control and be OK.

Thanks for all the help. I'll try that soon as I get a chance. :)

Hmmm don't seem to be having any joy. I'll keep looking around.

Aha! Success!

Thanks for all your help. I finally had success when I updated the InnerHtml within the webBrowser1_DocumentComplete() function generated automatically by double clicking on the webBrowser component :)

private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            string html = Init_Html();
            bool bError = true;
            while (bError)
            {
                try
                {
                    webBrowser1.Document.Body.InnerHtml = html;
                    bError = false;
                }
                catch (Exception ex)
                {
                    ;
                }
            }
        }

        private string Init_Html()
        {
            //format file, place into string temp
            return temp;
        }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.