开发者

Display an html formatted string

开发者 https://www.devze.com 2022-12-16 19:09 出处:网络
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'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开发者_StackOverflow中文版 it?

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

thanks


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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号