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!

Win Forms App to generate user formatted html

Status
Not open for further replies.

kjedwards

Programmer
Feb 14, 2002
12
GB
Hi

Don't really know how to categorise this request but I am looking for some kind of way to generate html files from text files that incorporating template/directives that will be auto filled by values stored in a db. It is a win forms app.

To give a very crude example.

File head.txt has say -

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "<html xmlns="<head>

<title>{{MY_TITLE}}[/color red]</title>

</head>

File body.txt has say -

<body>
{{MY_BODY}}[/color red]
</body>

File footer.txt has say -

</html>

I want to be able to read in head.txt identify the tag {{MY_TITLE}}[/color red] and replace it with a stored value.

Similarly for body.txt replace the {{MY_BODY}}[/color red] tags and footer.txt then write out a fully formed html file incorporated the values put in where the tags are.

I have seen several apps using this kind of thing so that end users can 'design' the html output from database apps but I have no idea how to do it or where to start looking.

Does anyone have any ideas?

Thanks

Kevin
 
Your basic html page is just a text file with a specific extension, either or html or htm normally. You can leverage the System.File.IO namespace to write to a text file and just save it with the proper extension. You need only to code out how the work goes...

So based on your example, you might have something like:

Code:
Using sw As new Streamwriter("C:\youfilename.html")
    sw.Writeline("<!DOCTYPE html PUBLIC """-//W3C//DTD XHTML 1.0 Transitional//EN""" """[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd""">")[/URL]
    sw.Writeline("<html xmlns="""[URL unfurl="true"]http://www.w3.org/1999/xhtml""">")[/URL]
    sw.Writeline("<head>")
    sw.Writeline("<title>" & youvariablefortitlehere & "</title>")
    sw.Writeline("</head>")
    sw.Writeline("<body>")
    sw.Write(yourvariableforbodyhere & Environment.NewLine) 
    sw.Writeline("</body>")
    sw.Writeline("</html> ")
    sw.Close ' This closes and saves the file....can't forget it!
End Using

I would use write for the body because it is mostly likely a longer block of data and probably built using a StringBuilder object. WriteLine auto-inserts a line break at the end, Write does not. This allows the body to be formatted as you wish while creating it and then just inserting it into the body tab when needed.

Hope that helps push you in the right direction.

=======================================
People think it must be fun to be a super genius, but they don't realize how hard it is to put up with all the idiots in the world. (Calvin from Calvin And Hobbs)

Robert L. Johnson III
CCNA, CCDA, MCSA, CNA, Net+, A+, CHDP
VB.NET Programmer
 
Hi

Thanks for that and I will have a look at the IO namespace.

I guess that will handle the read in as well?

Ideally the way I see this working is that the .txt or .html files are read in, the tags are recognised and substituted for values from a db and then the whole lot is written out again

Kevin
 
Yes....Streamreader and Stremawriter should fit your needs.

=======================================
People think it must be fun to be a super genius, but they don't realize how hard it is to put up with all the idiots in the world. (Calvin from Calvin And Hobbs)

Robert L. Johnson III
CCNA, CCDA, MCSA, CNA, Net+, A+, CHDP
VB.NET Programmer
 
Thanks again - I'll have a good read and then try to set up something simple and build it up from there
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top