htmlparser补全HTML,htmlparser

源程序代码

我们的 C# 程序中经常会产生一些数据,这些数据可以使用 Html 表格进行展现。现在让我们开始写相关的 C# 程序吧。下面就是 HtmlMaker.cs:01:usingSystem;02:usingSystem.IO;03:usingSystem.Net;04:usingSystem.Data;05:usingSystem.Drawing;06:usingSystem.Collections.Generic;07:08:namespaceSkyiv09:{10: public sealed classHtmlMaker11:{12: public boolTitleVisible { get; set; }13:14: stringtitle;15: DataViewview;16: IEnumerable> info;17:18: publicHtmlMaker(stringtitle, IEnumerable> info, DataViewview)19: {20: this.title = title;21: this.info = info;22: this.view = view;23: this.TitleVisible = true;24: }25:26: public voidSave(stringhtmlFileName)27: {28: using(varwriter = newStreamWriter(htmlFileName))29: {30: WriteHead(writer);31: WriteTitle(writer);32: WriteInfo(writer);33: newHtmlTable(view).Write(writer);34: WriteTail(writer);35: }36: }37:38: voidWriteTitle(TextWriterwriter)39: {40: if(!TitleVisible) return;41: writer.Write("

");42: WebUtility.HtmlEncode(title, writer);43: writer.WriteLine("

");44: }45:46: voidWriteInfo(TextWriterwriter)47: {48: if(info == null) return;49: vardt = newDataTable();50: dt.Columns.Add("Key", typeof(string));51: dt.Columns.Add("Value", typeof(string));52: foreach(varrow ininfo)53: {54: vardr = dt.NewRow();55: dr[0] = row.Item1;56: dr[1] = row.Item2;57: dt.Rows.Add(dr);58: }59: varhtmlTable = newHtmlTable(dt.DefaultView);60: htmlTable.ColumnHeadersVisible = false;61: htmlTable.Write(writer);62: }63:64: voidWriteHead(TextWriterwriter)65: {66: writer.Write(""-//W3C//DTD XHTML 1.0 Strict//EN\" ");67: writer.WriteLine("\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">");68: writer.WriteLine("");69: writer.WriteLine("");70: writer.WriteLine(" ");71: writer.Write(" ");72: WebUtility.HtmlEncode(title, writer);73: writer.WriteLine("");74: writer.WriteLine("");75: writer.WriteLine("");76: }77:78: voidWriteTail(TextWriterwriter)79: {80: writer.WriteLine("");81: writer.WriteLine("");82: }83: }84:}

注意上述程序第 70 行表明我们生成的 Html 文件是使用 UTF-8 编码,这应该成为一个共识。而在第 28 行使用的 StreamWriter 类默认情况也是使用 UTF8Encoding 。还有要注意第 42 行和第 72 行,在写入 Html 文件时要使用 WebUtility 类的静态方法 HtmlEncode 对数据进行编码。在第 33 行和第 59 行使用的 HtmlTable 类的源程序 HtmlTable.cs 如下所示:01:usingSystem;02:usingSystem.IO;03:usingSystem.Net;04:usingSystem.Data;05:usingSystem.Drawing;06:07:namespaceSkyiv08:{09: sealed classHtmlTable10:{11: public intLevel { get; set; }12: public boolColumnHeadersVisible { get; set; }13: publicColorColumnHeadersBackgroundColor { get; set; }14: publicColorBackgroundColor { get; set; }15: publicColorAlternatingRowsBackgroundColor { get; set; }16:17: DataViewview;18:19: publicHtmlTable(DataViewview)20: {21: this.view = view;22: Level = 1;23: ColumnHeadersVisible = true;24: ColumnHeadersBackgroundColor = Color.Cyan;25: BackgroundColor = Color.Azure;26: AlternatingRowsBackgroundColor = Color.LightYellow;27: }28:29: public voidWrite(TextWriterwriter)30: {31: if(writer == null) return;32: if(view == null) return;33: varblank = "".PadLeft(Level * 2);34: writer.Write(blank);35: writer.Write("

");44: }45:46: private voidWriteBackgroundColor(TextWriterwriter, Colorcolor)47: {48: if(color == Color.Empty) return;49: writer.Write(" style=\"background-color: ");50: writer.Write(color.ToHtmlCode());51: writer.Write("\"");52: }53:54: voidWriteTableHead(TextWriterwriter)55: {56: varblank2 = "".PadLeft((Level + 1) * 2);57: varblank3 = "".PadLeft((Level + 2) * 2);58: writer.Write(blank2);59: writer.Write("");62: foreach(DataColumncolumn inview.Table.Columns)63: {64: writer.Write(blank3);65: writer.Write("");66: WebUtility.HtmlEncode(column.Caption, writer);67: writer.WriteLine("");68: }69: writer.Write(blank2);70: writer.WriteLine("");71: }72:73: voidWriteTableRow(TextWriterwriter, DataRowViewview, introwIndex)74: {75: varblank2 = "".PadLeft((Level + 1) * 2);76: varblank3 = "".PadLeft((Level + 2) * 2);77: writer.Write(blank2);78: writer.Write("");81: foreach(varfield inview.Row.ItemArray)82: {83: writer.Write(blank3);84: writer.Write("");85: WebUtility.HtmlEncode(field.ToString(), writer);86: writer.WriteLine("");87: }88: writer.Write(blank2);89: writer.WriteLine("");90: }91: }92:}

几点说明:第 11 行的 Level 字段表示这个

第 12 行的 ColumnHeadersVisible 字段表示是否要显示该表格的标题行。

第 13 行的 ColumnHeadersBackgroundColor 字段表示该表格标题行的背景色,默认值是青色(Cyan)。

第 14 行的 BackgroundColor 字段表示该表格的背景色,默认值是天蓝色(Azure)。

第 15 行的 AlternatingRowsBackgroundColor 字段表示该表格偶数行的背景色,默认值是浅黄色(LightYellow)。

以上三个字段的值都可以设为 Color.Empty,表示继承上一级的背景色。

第 50 行的 ToHtmlCode 方法是一个扩展方法,定义于 Skyiv.ExtensionMethods 静态类中。

下面就是 ExtensionMethods.cs:01:usingSystem.Drawing;02:03:namespaceSkyiv04:{05: public static classExtensionMethods06:{07: public static stringToHtmlCode(thisColorcolor)08: {09: return string.Format("#{0:X2}{1:X2}{2:X2}", color.R, color.G, color.B);10: }11: }12:}

使用示例

这让我们来看一个例子吧,下面就是 EnumTester.cs:01:usingSystem;02:usingSystem.Data;03:usingSystem.Collections.Generic;04:05:namespaceSkyiv.Tester06:{07: sealed classEnumTester08:{09: static voidMain()10: {11: try12:{13: vartype = typeof(System.IO.FileAttributes);14: varhtml = newHtmlMaker("枚举测试者", GetInfo(type), GetTable(type).DefaultView);15: html.TitleVisible = false;16: html.Save("enum.html");17: }18: catch(Exceptionex)19: {20: Console.Error.WriteLine(ex);21: }22: }23:24: staticList> GetInfo(Typetype)25: {26: varinfo = newList>();27: info.Add(Tuple.Create("枚举类型名称", type.ToString()));28: info.Add(Tuple.Create("枚举成员数量", Enum.GetValues(type).Length.ToString()));29: returninfo;30: }31:32: staticDataTableGetTable(Typetype)33: {34: vartable = newDataTable();35: table.Columns.Add("十进制", typeof(string));36: table.Columns.Add("十六进制", typeof(string));37: table.Columns.Add("名称", typeof(string));38: foreach(varv inEnum.GetValues(type))39: {40: vardr = table.NewRow();41: dr[0] = Enum.Format(type, v, "D");42: dr[1] = Enum.Format(type, v, "X");43: dr[2] = Enum.Format(type, v, "G");44: table.Rows.Add(dr);45: }46: returntable;47: }48: }49:}

在 Windows Vista 操作系统的 .NET Fraemwork 4 环境中编译和运行:E:\work> csc EnumTester.cs HtmlMaker.cs HtmlTable.cs ExtensionMethods.csMicrosoft(R) Visual C# 2010 编译器 4.0.30319.1 版版权所有(C) Microsoft Corporation。保留所有权利。E:\work> EnumTesterE:\work>

这个 EnumTester.exe 运行后生成 enum.html 文件,在谷歌浏览器中的显示效果如下图所示:

29179354_1.jpg

这个 Html 文件的源代码如下所示:01:02:03:04: 05: 枚举测试者06:07:08: 09: 10: 枚举类型名称11: System.IO.FileAttributes12: 13: 14: 枚举成员数量15: 1416: 17:

18: 19: 20: 十进制21: 十六进制22: 名称23: 24: 25: 126: 0000000127: ReadOnly28: 29: 30: 231: 0000000232: Hidden33: 34: 35: 436: 0000000437: System38: 39: 40: 1641: 0000001042: Directory43: 44: 45: 3246: 0000002047: Archive48: 49: 50: 6451: 0000004052: Device53: 54: 55: 12856: 0000008057: Normal58: 59: 60: 25661: 0000010062: Temporary63: 64: 65: 51266: 0000020067: SparseFile68: 69: 70: 102471: 0000040072: ReparsePoint73: 74: 75: 204876: 0000080077: Compressed78: 79: 80: 409681: 0000100082: Offline83: 84: 85: 819286: 0000200087: NotContentIndexed88: 89: 90: 1638491: 0000400092: Encrypted93: 94: 95:96:
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值