views:

129

answers:

3

I'd like to display a string that containts html code (mainly for formating purposes like italic, bold, indentation, colors etc.) from a C# console application.

I don't think I'll need to go with a WebBrowser class for that, since there won't be any kind of navigation possible.

1) What would be the most straightforward way to do it?

2) Would it be possible to display it too in the console using the hmtl formating?

thanks

+2  A: 

There is no support for html in the Console. You can set the foreground and background color of the console, but you can't use italics or bold font. At best, you could write an html interpreter (or hack something up with the classes in the System.Xml namespace) to allow you to use colors in the console.

It sounds like you might actually be using Winforms, since you reference the WebBrowser control. If so, you could try using a read-only RichTextBox or the WebBrowser control, or if you need an absolutely lightweight solution, you could implement your own control which handles the parts of html you need.

Zach Johnson
I shudder at the thought of hand-rolling any type of HTML parser.
Chris
Html Agility Pack seems to be the most popular .NET HTML parser. http://codeplex.com/htmlagilitypack
Josh Einstein
I -very- bare bones parser to handle just a couple simple tags would be pretty simple to do using a recursive decent techniques.
Foredecker
A: 

You could use the HTML Control for rendering, if you want.

You just plop it on a windows form, and it wraps MSHTML, doing the rendering for you.

Keep in mind that HTML rendering is not an easy thing. Gecko, WebKit, and MSHTML are HUGE codebases. You are better off using one of those rather than trying to roll your own.

John Gietzen
@John, I almost missed it too, but he's talking about displaying formatted text in the Command Prompt (Console window) not Windows Forms.
Ash
@Ash, in fact, it could be both. I'll pick the most easy-to-implement solution.
Stringer Bell
+1  A: 

Using an existing HTML rendering engine would be very expensive from a performance perspective - especially memory.

The other issue you face is that most HTML formatting will not translate to the console - the only effects you really have available is foreground and background color.

Your best bet is to write a simple parser your self. You will need to decide how to interpret things like bold and italic using console colors.

Since you only need to handle two possibly three HTML tags the parsing should be very simple, the logic to ignore other HTML tags should be pretty straight forward.

Note, HTML is not a regular language so you cannot effectively use regular expressions for parsing. I would recommend a simple recursive decent parser - these are straight forward to implement. You could also write a state machine, but it would need to have some recursive or stack semantics to be corret.

Foredecker