使用StringBuilder在C#中构造HTML的问题 [英] Problems using a StringBuilder to construct HTML in C#
本文介绍了使用StringBuilder在C#中构造HTML的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这行代码形成一个HTML字符串:
StringBuilder builder = new StringBuilder();
pre>
builder.Append(< a href ='#'onclick =);
builder.Append((char)22); //builder.Append('\');
builder.Append(diagnosticSelected('+ obj.Id +','+ obj.Name +'));
builder.Append((char)22);
builder.Append(>+ obj.Name +< / a>);
在浏览器中我得到
< a href = '#'onclick = \diagnosticSelected('id','text')\>某些文本这里< / a>
,因为\我收到错误。如何输出一个?
解决方案很有趣的是,我看到人们使用StringBuilder完全错过了他们的观点。 StringBuilder上还有另一种方法叫做AppendFormat,它可以帮助你很多:
builder.AppendFormat(< a href = #'onclick = \foo('{0}','{1}')\> {2}< / a>,var1,var2,var3);
希望这有帮助,
I have this line of code that forms an html string:
StringBuilder builder = new StringBuilder(); builder.Append("<a href='#' onclick="); builder.Append((char)22); //builder.Append('\"'); builder.Append("diagnosisSelected('" + obj.Id + "', '" +obj.Name + "')"); builder.Append((char)22); builder.Append(" >" + obj.Name + "</a>");
In the browser I get
<a href='#' onclick=\"diagnosisSelected('id', 'text')\" >some text here</a>
and I get an error because of \". How can I output a "?
解决方案It's funny how many times I see people use StringBuilder yet completely miss the point of them. There's another method on StringBuilder called AppendFormat which will help you a lot here:
builder.AppendFormat("<a href='#' onclick=\"foo('{0}','{1}')\">{2}</a>", var1, var2, var3);
Hope this helps,
这篇关于使用StringBuilder在C#中构造HTML的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文