1、htmlCheck类

using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Diagnostics;
namespace htmlCheck
{
/// <summary>
/// Asp.Net检查HTML是否闭合以及自动修复 http://webapi.cnblogs.com/
/// </summary>
class TagsList
{
private ArrayList data; public int Size
{
get
{
return data.Count;
}
} public TagsList()
{
data = new ArrayList();
} public void add(String str)
{
data.Add(str);
} public string get(int index)
{
if (index < data.Count)
return (string)data[index];
else
return null;
} public bool remove(string str)
{
if (data.IndexOf(str) == -) return false;
data.Remove(str);
return true;
} public void remove(int index)
{
data.RemoveAt(index);
}
} public class TagsChecker
{
/// <summary>
/// 检查html标签是闭合
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static bool check(string str)
{
TagsList[] unclosedTags = getUnclosedTags(str); if (unclosedTags[].Size != )
{
return false;
}
for (int i = ; i < unclosedTags[].Size; i++)
{
if (unclosedTags[].get(i) != null)
return false;
} return true;
}
/// <summary>
/// 处理未闭合的html代码
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static string fix(String str)
{
StringBuilder fixeds = new StringBuilder(); // 存放修复后的字符串
TagsList[] unclosedTags = getUnclosedTags(str); // 生成新字符串
for (int i = unclosedTags[].Size - ; i > -; i--)
{
fixeds.Append("<" + unclosedTags[].get(i) + ">");
} fixeds.Append(str); for (int i = unclosedTags[].Size - ; i > -; i--)
{
String s = null;
if ((s = unclosedTags[].get(i)) != null)
{
fixeds.Append("</" + s + ">");
}
} return fixeds.ToString();
} private static TagsList[] getUnclosedTags(String str)
{
StringBuilder temp = new StringBuilder(); // 存放标签
TagsList[] unclosedTags = new TagsList[];
unclosedTags[] = new TagsList(); // 前不闭合,如有</div>而前面没有<div>
unclosedTags[] = new TagsList(); // 后不闭合,如有<div>而后面没有</div>
bool flag = false; // 记录双引号"或单引号'
char currentJump = ' '; // 记录需要跳过''还是"" char current = ' ', last = ' '; // 当前 & 上一个 // 开始判断
for (int i = ; i < str.Length; )
{
current = str[i++]; // 读取一个字符
if (current == '"' || current == '\'')
{
flag = flag ? false : true; // 若为引号,flag翻转
currentJump = current;
if (flag)
{
while (i < str.Length && str[i++] != currentJump)
; // 跳过引号之间的部分
flag = false;
}
}
else if (current == '<')
{ // 开始提取标签
current = str[i++];
if (current == '/')
{ // 标签的闭合部分,如</div>
current = str[i++]; // 读取标签
while (i < str.Length && current != '>')
{
temp.Append(current);
current = str[i++];
} // 从tags_bottom移除一个闭合的标签
if (!unclosedTags[].remove(temp.ToString()))
{ // 若移除失败,说明前面没有需要闭合的标签
unclosedTags[].add(temp.ToString()); // 此标签需要前闭合
}
temp.Remove(, temp.Length); // 清空temp
}
else
{ // 标签的前部分,如<div>
last = current;
while (i < str.Length && current != ' '
&& current != ' ' && current != '>')
{
temp.Append(current);
last = current;
current = str[i++];
} // 已经读取到标签,跳过其他内容,如<div id=test>跳过id=test
while (i < str.Length && current != '>')
{
last = current;
current = str[i++];
if (current == '"' || current == '\'')
{ // 判断双引号
flag = flag ? false : true;
currentJump = current;
if (flag)
{ // 若引号不闭合,跳过到下一个引号之间的内容
while (i < str.Length && str[i++] != currentJump)
;
current = str[i++];
flag = false;
}
}
}
if (last != '/' && current == '>') // 判断这种类型:<TagName />
unclosedTags[].add(temp.ToString());
temp.Remove(, temp.Length);
}
}
}
return unclosedTags;
}
}
}

2、使用方法

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using htmlCheck;//注意,添加这个引用
using System.Text.RegularExpressions; public partial class ceshi : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string str1 = "<p>tt<table><tr><td>webapi</td></tr><tr><td>323</td></p>";
string str2 = "tt<u>ss</u><div id=test name=\"<test>\"><a>fds</a></div>";
Response.Write("\r\n检查文本 " + str1);
Response.Write("\r\n结果:" + TagsChecker.check(str1));
Response.Write("\r\n检查文本 " + str2);
Response.Write("\r\n结果:" + TagsChecker.check(str2));
Response.Write("\r\n修复文本 " + str1);
Response.Write("\r\n结果:" + TagsChecker.fix(str1)); }
}

说明:转载的,已测试,确实闭合修复了,但是闭合的位置不正确

Asp.Net检查HTML是否闭合以及自动修复的更多相关文章

  1. asp.net 检查文件夹和文件是否存在

    原文  asp.net 检查文件夹和文件是否存在 允许 path 参数指定相对或绝对路径信息. 相对路径信息被解释为相对于当前工作目录. 检查该目录是否存在之前,从 path 参数的末尾移除尾随空格. ...

  2. asp.net检查验证字符串是否为纯数字方法小结

    原文  asp.net检查验证字符串是否为纯数字方法小结 在asp.net中验证字符串是不是为数字我们没有像php中那么多丰富的函数来直接使用,这里我整理了一些比较实例的验证字符串是否为纯数字方法代码 ...

  3. asp.net检查服务器上目录或文件是否存在示例

    原文 asp.net检查服务器上目录或文件是否存在示例 asp.net为我们提供了文件系统对象了,对于目录与文件判断是否存在我们有System.IO.File.Exists与System.IO.Dir ...

  4. asp.net C#检查URL是否有效

    我们有时候需要对用户输入的网站(URL)进行有效性检查,  代码如下 复制代码 function CheckUrl(str) {    var RegUrl = new RegExp();    Re ...

  5. ASP.NET(转自wiki)

    ASP.NET是由微软在.NET Framework框架中所提供,开发Web应用程序的类库,封装在System.Web.dll文件中,显露出System.Web名字空间,并提供ASP.NET网页处理. ...

  6. .net学习笔记----Asp.net的生命周期之一应用程序生命周期

    Http请求刚刚到达服务器的时候 当服务器接收到一个 Http请求的时候,IIS (Internet Information Services,互联网信息服务)首先需要决定如何去处理这个请求. 什么是 ...

  7. ASP连接access 数据库的增删改查 - imsoft.cnblogs

    假设数据库文件名叫data.mdb里面有2个表:1.admin2.news假设admin是保存用户名和密码,里面有字段:UserName,PassWord.假设我们要在判断一个用户名叫name,密码是 ...

  8. ASP.NET 应用程序生命周期概述[转自MSDN]

    本文转自:http://msdn.microsoft.com/zh-cn/library/ms178473(VS.80).aspx 下表描述了 ASP.NET 应用程序生命周期的各个阶段.   阶段 ...

  9. asp.net 文件上传示例整理

    ASP.NET依托.net framework类库,封装了大量的功能,使得上传文件非常简单,主要有以下三种基本方法. 方法一:用Web控件FileUpload,上传到网站根目录.  代码如下 复制代码 ...

随机推荐

  1. GPS(Global Positioning System)全球定位系统

    GPS构成: 1.空间部分 GPS的空间部分是由24 颗工作卫星组成,它位于距地表20 200km的上空,均匀分布在6 个轨道面上(每个轨道面4 颗) ,轨道倾角为55°.此外,还有4 颗有源备份卫星 ...

  2. 快递查询API接口对接方法

    各类接口 快递查询API有即时查询和订阅查询两种,即时是请求即返回数据,订阅则是订阅快递单号到接口,有物流轨迹更新则全量返回数据.目前常用的有快递鸟.快递100.快递网等. 快递鸟即时API可以查询3 ...

  3. js重写原型对象

    首先看两段很相似的代码: 1. function Person(){} Person.prototype = { constructor:Person, name:"Nic", a ...

  4. RESTful HTTP实践

    http://www.infoq.com/cn/articles/designing-restful-http-apps-roth 摘要: 本文对RESTful HTTP的基础原理做了一个概览,探讨了 ...

  5. LC-检索

    line void LC(tree T,float cost) { //为找一个答案结点检索T if(T是答案结点) {输出T:return:} E=T: //E-结点 将活结点表初始化为空: ) { ...

  6. JS学习笔记——标准对象

    一.对象 在js中万物皆对象. 二.对象类型 number.string.boolean.undefined.function.object等 用typeof来获取对象的类型 如: alert( ty ...

  7. poj3660 Cow Contest(Floyd-Warshall方法求有向图的传递闭包)

    poj3660 题意: 有n头牛, 给你m对关系(a, b)表示牛a能打败牛b, 求在给出的这些关系下, 能确定多少牛的排名. 分析: 在这呢先说一下关系闭包: 关系闭包有三种: 自反闭包(r), 对 ...

  8. oc语言学习之基础知识点介绍(二):类和对象的进一步介绍

    一.类.对象在内存中的存储 /* 内存分区: 栈:局部变量 堆:程序员自己写代码申请开辟的 程序员自己维护,编译器现在帮我们自动优化了,它在合适的给我们加上了释放空间的语句,所以我们现在写的对象不会造 ...

  9. SQL Server 错误检测与修复

    简介 在一个理想的世界中,不会存在任何数据库的损坏,就像我们不会将一些严重意外情况列入我们生活中的日常一样,而一旦这类事情发生,一定会对我们的生活造成非常显著的影响,在SQL Server中也同样如此 ...

  10. ubuntu系统安装flashplayer

    打开浏览器,输入adobe flashplayer 进入官方网站,下载Linux 32-bit, 简体中文, Firefox,下载.tar.gz包. 然后点击立即下载.下载之后找到解压该文件夹,找到 ...