HtmlTextWriter使用動機
最近剛好在研究html由程式產生,發現在.net framework下有一個實用簡單的類別-HtmlTextWriter,藉由這個類別可以快速的由程式中寫出Html碼,命名空間是 System.Web.UI,如果在WinForm下使用,請引用(Reference)System.Web.dll。
HtmlTextWriter與Html32TextWriter
HtmlTextWriter支援Html4.0標準,而Html32TextWriter支援Html3.2標準,在msdn中一般不建議將採用Html32TextWriter,它為支援低版本的瀏覽器而設計的,所以建議採用HtmlTextWriter物件,由名稱上也可猜測這類別比較正統,沒有多餘的贅字。
TextWriter - StringWriter, StreamWriter 簡介
在介紹HtmlTextWriter使用前,必須先介紹一些相關的類別,因為HtmlTextWriter本身並不支援輸出的功能,必須由TextWriter的類別來實現輸出的功能。
TextWriter: 表示可以寫入一連串連續字元的寫入器,這個類別是抽象的。
StringWriter: 實作TextWriter以便將資訊寫入字串。資訊儲存在基礎 StringBuilder 中。
StreamWriter: 實作以特定的編碼方式將字元寫入位元組資料流的TextWriter。
HtmlTextWriter初始化
HtmlTextWriter在建構時必須要接串接TextWriter,本身不提供輸出的方法或屬性。
建構子: public HtmlTextWriter (TextWriter writer)
輸出成檔案
System.IO.StreamWriter streamWriter = new System.IO.StreamWriter("C:\\htmlSample.htm");
System.Web.UI.HtmlTextWriter htmlWriter = new System.Web.UI.HtmlTextWriter(streamWriter);
streamWriter.toString();
streamWriter.Close();
在C:下可看到htmlSample.htm檔案
輸出成字串:
StringWriter strWriter=new System.IO.StringWriter();
HtmlTextWriter htmlWriter=new HtmlTextWriter(strWriter);
獲得輸出結果 string result = strWriter.toString();
HtmlTextWriter常用方法
1. void RenderBeginTag(HtmlTextWriterTag tagKey)
用於Html開始標籤,如<body>,HtmlTextWriterTag是列舉類別,內部有所有Html 4.0的列舉標籤,如果要增加body標籤,就是RenderBeginTag(HtmlTextWriterTag.Body)
2. void RenderEndTag()
與RenderBeginTag對應,有幾個RenderBeginTag就必須有幾個RenderEndTag。如下面的例子:
<html>
<head></head>
<body></body>
</html>
上面一段是html基本架構,使用HtmlTextWriter生成就是
htmlWriter.RenderBeginTag(HtmlTextWriterTag.Html);
htmlWriter.RenderBeginTag(HtmlTextWriterTag.Head);
htmlWriter.RenderEndTag(); //End of Head
htmlWriter.RenderBeginTag(HtmlTextWriterTag.Body);
htmlWriter.RenderEndTag(); //End of Body
htmlWriter.RenderEndTag(); //End of Html
3. void AddAttribute()
用於增加標籤的屬性,例如<img>標籤的url屬性、width屬性等。注意:AddAttribute必須出現在RenderBeginTag的前面
ex:
htmlWriter.AddAttribute("url","../xxx.gif");
htmlWriter.AddAttribute("width","50");
htmlWriter.AddAttribute("height","50");
htmlWriter.RenderBeginTag(HtmlTextWriterTag.Img);
htmlWriter.RenderEndTag();
輸出: <img url="../xxx.gif" width="50" height="50" />。
4.void AddStyleAttribute()
由於Html並不是很嚴謹的語言,所以上面的函式就可實作所須輸出的字串,但建議還是採用以下範例,以下3個例子,所產生的字串都相同,也能正確輸出
(a)
htmlWriter.AddAttribute("style", "margin:0px; color: #ff0; overflow: auto");
(b)
htmlWriter.AddStyleAttribute("margin", "0px");
htmlWriter.AddStyleAttribute("color", "#ff0");
htmlWriter.AddStyleAttribute("overflow", "auto");
(c)
htmlWriter.AddStyleAttribute(System.Web.UI.HtmlTextWriterStyle.Margin, "0px");
htmlWriter.AddStyleAttribute(System.Web.UI.HtmlTextWriterStyle.Color, ""#ff0");
htmlWriter.AddStyleAttribute(System.Web.UI.HtmlTextWriterStyle.Overflow, "auto");
輸出: Style="margin:0px; color: #ff0; overflow: auto"
我採用的是第三種方法,是因為它有提供對應的列舉,不想全部手動自己寫。
5. void Write()和void WriteLine()
產生Html標籤以外的所有信息。
此外,若是從WinForm上的顏色轉換到網頁上的顏色,可採用此函式System.Drawing.ColorTranslator.ToHtml(Color c);
實作 Web 跑馬燈
private void SaveMessageToHtml1(string htmPath)
{
System.IO.StreamWriter streamWriter = new System.IO.StreamWriter(htmPath);
System.Web.UI.HtmlTextWriter htmlWriter = new System.Web.UI.HtmlTextWriter(streamWriter);
// Html
htmlWriter.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Html);
// Head
htmlWriter.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Head);
htmlWriter.RenderEndTag(); // End of Head
// Meta
htmlWriter.AddAttribute("http-equiv", "content-type");
htmlWriter.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Content, "text/html; charset=UTF-8");
htmlWriter.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Meta);
htmlWriter.RenderEndTag(); // End of Meta
// Body
htmlWriter.AddStyleAttribute(System.Web.UI.HtmlTextWriterStyle.FontFamily, "Arial");
htmlWriter.AddStyleAttribute(System.Web.UI.HtmlTextWriterStyle.Margin, "0px");
htmlWriter.AddStyleAttribute(System.Web.UI.HtmlTextWriterStyle.Color, System.Drawing.ColorTranslator.ToHtml(Color.Yellow));
htmlWriter.AddStyleAttribute(System.Web.UI.HtmlTextWriterStyle.BackgroundColor, System.Drawing.ColorTranslator.ToHtml(Color.Black));
htmlWriter.AddStyleAttribute(System.Web.UI.HtmlTextWriterStyle.Overflow, "auto");
htmlWriter.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Body);
// Marquee
htmlWriter.AddStyleAttribute(System.Web.UI.HtmlTextWriterStyle.Position, "relative");
htmlWriter.AddStyleAttribute(System.Web.UI.HtmlTextWriterStyle.Top, "25%");
htmlWriter.AddStyleAttribute(System.Web.UI.HtmlTextWriterStyle.Height, "100%");
htmlWriter.AddStyleAttribute(System.Web.UI.HtmlTextWriterStyle.FontSize, string.Format("{0}px", "20"));
htmlWriter.AddStyleAttribute(System.Web.UI.HtmlTextWriterStyle.Overflow, "hidden");
htmlWriter.AddAttribute("behavior", "scroll");
htmlWriter.AddAttribute("direction", "left");
htmlWriter.AddAttribute("loop", "-1");
htmlWriter.AddAttribute("scrollamount", this.scrollOffset.Text);
htmlWriter.AddAttribute("scrolldelay", this.scrollDelay.Text);
htmlWriter.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Marquee);
htmlWriter.Write("Hello! This is Web Brower-Marquee");
htmlWriter.WriteLine();
htmlWriter.RenderEndTag(); // End of Marquee
htmlWriter.RenderEndTag(); // End of Body
htmlWriter.RenderEndTag(); // End of Html
streamWriter.ToString();
streamWriter.Close();
htmlWriter.Close();
streamWriter.Dispose();
htmlWriter.Dispose();
// 除錯用
System.Diagnostics.Process.Start("IEXPLORE.EXE", htmPath);
}
Reference:
http://www.cnblogs.com/tonyqus/archive/2005/02/15/104576.html
MSDN
0 意見:
張貼留言