locked
Editing HTML tags at run-time RRS feed

  • Question


  • Hello,

    I am trying to achieve something a bit tricky. I have a web application that displays news bar from an external HTML file. I need to enter text at this HTML tag so as to update the news bar. How can I edit HTML tag/code from VB code at run time. I am using VS 2005. Below is an the HTML file contents. What I need is to change the text "HELLO WORLD" to whatever I want. Please help.

    Thanks

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
    <style>
    .ticker {
    font: bold 26px sans-serif;
    color: blue;
    }
    </style>
    </head>
    <body onload="init();">
    <pre class="ticker" id="tickerLine">Hello World.<br></pre>
    </body>
    </html>

    Tuesday, October 2, 2007 9:14 AM

Answers

  • Hi,

     

    You can use the DOM that is build in to the Web Browser control and edit the document as you would using client side scripts. Here is an example that demonstrates how you can change the text in the tickerLine pre element.

     

    Dim browser As New System.Windows.Forms.WebBrowser

    browser.Navigate("about:blank")

    browser.Document.Write("<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN'><html><head><style>.ticker {font: bold 26px sans-serif;color: blue;} </style></head><body onload='init();'><pre class='ticker' id='tickerLine'>Hello World.<br></pre></body></html>")

     

    Console.WriteLine(browser.Document.GetElementById("tickerLine").InnerText) 'display the Hello World

     

    browser.Document.GetElementById("tickerLine").InnerText = "Goodbye"    'change the Hello World to Goodbye

    Console.WriteLine(browser.Document.GetElementById("tickerLine").InnerText)   'display the change

     

    Console.ReadLine()

     

    If you are already displaying the document in a web browser then you don't need to worry about setting the browser up. Just make calls against the browser.Document object.

    Tuesday, October 2, 2007 12:51 PM

All replies

  • Hi,

     

    You can use the DOM that is build in to the Web Browser control and edit the document as you would using client side scripts. Here is an example that demonstrates how you can change the text in the tickerLine pre element.

     

    Dim browser As New System.Windows.Forms.WebBrowser

    browser.Navigate("about:blank")

    browser.Document.Write("<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN'><html><head><style>.ticker {font: bold 26px sans-serif;color: blue;} </style></head><body onload='init();'><pre class='ticker' id='tickerLine'>Hello World.<br></pre></body></html>")

     

    Console.WriteLine(browser.Document.GetElementById("tickerLine").InnerText) 'display the Hello World

     

    browser.Document.GetElementById("tickerLine").InnerText = "Goodbye"    'change the Hello World to Goodbye

    Console.WriteLine(browser.Document.GetElementById("tickerLine").InnerText)   'display the change

     

    Console.ReadLine()

     

    If you are already displaying the document in a web browser then you don't need to worry about setting the browser up. Just make calls against the browser.Document object.

    Tuesday, October 2, 2007 12:51 PM
  • Hi Derek..

    Thanks for the example. I still need to save the changed to the file locally on disk. The changes seems to be only in memory right. Any ideas how I can do it.

    Thanks a lot
    Sunday, October 7, 2007 12:59 PM