社区
C#
帖子详情
如何进行 HTML 编码和解码?
Mister
2007-01-20 10:08:03
在非引用 System.Web.dll 情况下,即非 Web 应用程序下,有没有什么类能进行 HTML 编码和解码?
Server.HtmlEncode() ..... 之类的答案,请不要回帖!
...全文
2279
11
打赏
收藏
如何进行 HTML 编码和解码?
在非引用 System.Web.dll 情况下,即非 Web 应用程序下,有没有什么类能进行 HTML 编码和解码? Server.HtmlEncode() ..... 之类的答案,请不要回帖!
复制链接
扫一扫
分享
转发到动态
举报
写回复
配置赞助广告
用AI写文章
11 条
回复
切换为时间正序
请发表友善的回复…
发表回复
打赏红包
「已注销」
2007-02-12
打赏
举报
回复
up
flyin2006
2007-02-12
打赏
举报
回复
public static string HtmlDecode(string s)
{
if (s == null)
{
return null;
}
if (s.IndexOf('&') < 0)
{
return s;
}
StringBuilder builder1 = new StringBuilder();
StringWriter writer1 = new StringWriter(builder1);
HttpUtility.HtmlDecode(s, writer1);
return builder1.ToString();
}
public static void HtmlDecode(string s, TextWriter output)
{
if (s != null)
{
if (s.IndexOf('&') < 0)
{
output.Write(s);
}
else
{
int num1 = s.Length;
for (int num2 = 0; num2 < num1; num2++)
{
char ch1 = s[num2];
if (ch1 == '&')
{
int num3 = s.IndexOfAny(HttpUtility.s_entityEndingChars, num2 + 1);
if ((num3 > 0) && (s[num3] == ';'))
{
string text1 = s.Substring(num2 + 1, (num3 - num2) - 1);
if ((text1.Length > 1) && (text1[0] == '#'))
{
try
{
if ((text1[1] == 'x') || (text1[1] == 'X'))
{
ch1 = (char) ((ushort) int.Parse(text1.Substring(2), NumberStyles.AllowHexSpecifier));
}
else
{
ch1 = (char) ((ushort) int.Parse(text1.Substring(1)));
}
num2 = num3;
}
catch (FormatException)
{
num2++;
}
catch (ArgumentException)
{
num2++;
}
}
else
{
num2 = num3;
char ch2 = HtmlEntities.Lookup(text1);
if (ch2 != '\0')
{
ch1 = ch2;
}
else
{
output.Write('&');
output.Write(text1);
output.Write(';');
goto Label_0103;
}
}
}
}
output.Write(ch1);
Label_0103:;
}
}
}
}
flyin2006
2007-02-12
打赏
举报
回复
帮你反编译下:
public static string HtmlEncode(string s)
{
if (s == null)
{
return null;
}
int num1 = HttpUtility.IndexOfHtmlEncodingChars(s, 0);
if (num1 == -1)
{
return s;
}
StringBuilder builder1 = new StringBuilder(s.Length + 5);
int num2 = s.Length;
int num3 = 0;
Label_002A:
if (num1 > num3)
{
builder1.Append(s, num3, num1 - num3);
}
char ch1 = s[num1];
if (ch1 > '>')
{
builder1.Append("&#");
builder1.Append(ch1.ToString(NumberFormatInfo.InvariantInfo));
builder1.Append(';');
}
else
{
char ch2 = ch1;
if (ch2 != '"')
{
switch (ch2)
{
case '<':
builder1.Append("<");
goto Label_00D5;
case '=':
goto Label_00D5;
case '>':
builder1.Append(">");
goto Label_00D5;
case '&':
builder1.Append("&");
goto Label_00D5;
}
}
else
{
builder1.Append(""");
}
}
Label_00D5:
num3 = num1 + 1;
if (num3 < num2)
{
num1 = HttpUtility.IndexOfHtmlEncodingChars(s, num3);
if (num1 != -1)
{
goto Label_002A;
}
builder1.Append(s, num3, num2 - num3);
}
return builder1.ToString();
}
flyin2006
2007-02-12
打赏
举报
回复
我教你一招,用Reflector反编译System.Web.dll,看看HtmlEncode怎么实现的
或者直接用其代码 ;-)
james_hunter
2007-02-12
打赏
举报
回复
偏要用Server.HtmlEncode() ..... 之类的答案回复!
咬我!
he_8134
2007-02-12
打赏
举报
回复
同意楼上...
winner2050
2007-02-12
打赏
举报
回复
不是web程序也能用System.Web.dll
何必要虐待自己呢.
tgl10
2007-02-12
打赏
举报
回复
自己看下编码算法吧,不是很难
Mister
2007-02-12
打赏
举报
回复
来个人结贴,谢谢。
Mister
2007-01-22
打赏
举报
回复
来人啊~~~
Mister
2007-01-21
打赏
举报
回复
.....................................................
Html
中url的
编码
和
解码
博客介绍了
HTML
中URL的
编码
和
解码
,主要涉及三种方式:escape和unescape,对特定字符外的字符
编码
;encodeURI和decodeURI,用于对整个URL
编码
;encodeURIComponent和decodeURIComponent,对URL组成部分个别
编码
。并给出了相应的
编码
和
解码
示例。
js
html
编码
和
解码
,JavaScript字符集
编码
与
解码
本文深入探讨字符集、字符
编码
以及浏览器中的
HTML
、CSS和JavaScript
编码
解码
机制。重点介绍了ASCII、Unicode、UTF-8、
HTML
属性中的进制表示、CSS中的十六进制
编码
以及JavaScript的
编码
函数。同时,阐述了浏览器如何自动
解码
,如
HTML
中的16进制
编码
。最后,提到了JavaScript中的escape、encodeURI和encodeURIComponent三个
编码
函数的区别。
如何将
html
内容
解码
,3.5.3 对
HTML
进行
编码
和
解码
本文介绍了ASP.NET中如何使用Server对象的
Html
Encode和
Html
Decode方法
进行
HTML
文本的
编码
与
解码
。通过实例展示了如何在页面内容中控制显示效果,包括创建Web项目、添加控件及编写事件处理程序。
html
转义字符
解码
,js对
html
转义和反转义以及
编码
和
解码
本文分享了如何在项目中利用jQuery
进行
HTML
数据的
编码
和
解码
,包括
html
Encode、
html
Decode函数示例,以及实际场景中的应用。适合理解
HTML
转义和
编码
解码
在前后端交互中的重要性。
js 修改
html
编码
,Javascript 中对
HTML
编码
和
解码
的方法
这段代码展示了在JavaScript中如何实现对
HTML
的
编码
和
解码
。通过创建一个`div`元素,利用`textContent`或`innerText`属性,将字符串内容设置或获取,从而达到
编码
和
解码
的效果。这种方法兼容了Firefox和IE浏览器。此外,还提到了一些DOM属性如`scrollHeight`、`scrollLeft`等,以及JavaScript事件`onbeforeunload`。
C#
111,132
社区成员
642,541
社区内容
发帖
与我相关
我的任务
C#
.NET技术 C#
复制链接
扫一扫
分享
社区描述
.NET技术 C#
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
让您成为最强悍的C#开发者
试试用AI创作助手写篇文章吧
+ 用AI写文章