C#格式化HTML代码帮助类 HtmlFormater

.Net技术 码拜 10年前 (2015-07-10) 3126次浏览 0个评论

很多在线网站提供格式化HTML代码的工具,那么如何在C#中集成格式化HTML代码的功能呢?如下代码提供C#格式化HTML代码帮助类:

public class HtmlFormater
{
public static string ConvertToXml(string str, bool bLineAndIndent)
{
XmlDocument document1 = ConvertToXmlDocument(str);

if (bLineAndIndent)
{

StringBuilder builder1 = new StringBuilder();
XmlTextWriter writer1 = new XmlTextWriter(new StringWriter(builder1));
writer1.IndentChar = ‘ ‘;
writer1.Indentation = 4;
writer1.Formatting = Formatting.Indented;
document1.DocumentElement.WriteContentTo(writer1);
return builder1.ToString();
}
return document1.DocumentElement.InnerXml;
}

public static XmlDocument ConvertToXmlDocument(string str)
{
XmlDocument document1 = new XmlDocument();
document1.LoadXml(“<xml/>”);
SafeHtmlParser.ParseHtml(document1.DocumentElement, str);
return document1;
}
}
/// <summary>
/// SafeHtmlParser 的摘要说明。
/// </summary>
public class SafeHtmlParser
{
internal class ca
{
internal static Hashtable r;
}

// Methods
static SafeHtmlParser()
{
char[] chArray1 = new char[2] { ‘&’, ‘;’ };
SafeHtmlParser.fh = chArray1;
}
private SafeHtmlParser()
{
}
private string a()
{
StringBuilder builder1 = new StringBuilder();
int num1 = this.fg;
builder1.Append(this.e());
this.fg = num1;
if (builder1.Length == 0)
{
return null;
}
return builder1.ToString();
}
private string a(char A_0)
{
if (this.fg >= this.ff)
{
return “”;
}
int num1 = this.fd.IndexOf(A_0, this.fg);
if (num1 == -1)
{
return this.e();
}
int num2 = this.fd.IndexOf(‘<‘, this.fg, (int)(num1 – this.fg));
if (num2 != -1)
{
num1 = num2;
}
string text1 = this.fd.Substring(this.fg, num1 – this.fg);
this.fg = num1 + 1;
return text1;
}
private string a(out string A_0)
{
int num1 = this.fg;
A_0 = this.d();
string text1 = this.b();
this.fg = num1;
return text1;
}

……//由于代码太长了,只能粘贴部分。请需要的朋友到此处下载:C#格式化HTML代码帮助类下载 密码: pmxq

}

调用方式:richTextBox_content.Text = HtmlFormater.ConvertToXml(_content, true);


CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明C#格式化HTML代码帮助类 HtmlFormater
喜欢 (0)
[1034331897@qq.com]
分享 (0)

文章评论已关闭!