为什么使用TagBuilder而不是StringBuilder的? [英] Why use TagBuilder instead of StringBuilder?

查看:236
本文介绍了为什么使用TagBuilder而不是StringBuilder的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是在使用标记建设者和字符串生成器在HtmlHelper类创建一个表,或使用HTMLTABLE区别?

what's the difference in using tag builder and string builder to create a table in a htmlhelper class, or using the HtmlTable?

是不是又发生同样的事情??

aren't they generating the same thing??

推荐答案

TagBuilder 是专门为创建HTML标记及其内容而设计的类。你是正确的说,结果将是无论如何一个字符串,当然你仍然可以使用的StringBuilder ,结果将是相同的,但你可以做的事情更容易 TagBuilder 。比方说,你需要生成一个标记:

TagBuilder is a class that specially designed for creating html tags and their content. You are right saying that result will be anyway a string and of course you still can use StringBuilder and the result will be the same, but you can do things easier with TagBuilder. Lets say you need to generate a tag:

<a href='http://www.stackoverflow.com' class='coolLink'/>

使用的StringBuilder 你需要写是这样的:

var sb = new StringBuilder();
sb.Append("<a href='");
sb.Append(link);
sb.Append("' class = '");
sb.Append(ccsClass);
sb.Append("'/>");
sb.ToString();

这是不是非常酷,不是吗?
和比较如何使用它建立 TagBuilder ;

var tb = new TagBuilder("a");
tb.MergeAttribute("href",link);
tb.AddCssClass(cssClass);
tb.ToString(TagRenderMode.SelfClosing);

这不是更好吗?

这篇关于为什么使用TagBuilder而不是StringBuilder的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆