【问题标题】:Error caused by special characters in ITextSharpITextSharp 中的特殊字符导致的错误
【发布时间】:2012-07-07 20:03:45
【问题描述】:

我一直在使用 itextsharp,它运行良好,直到我将编辑器更改为 Telerik MVC 编辑器,如果我输入特殊字符,例如 <>,我会收到以下错误:

对象引用未设置为对象的实例。

我的程序的工作方式是使用以下脚本从 SQL Server 表中读取建议列表:

string BPRecommendation = "<span style='font-size:10;'>";
for (int i = 0; i < this.selectedVisit.Recommendations.Count; i++)
{
    if (i > 0) BPRecommendation += "<br />";
    BPRecommendation += this.selectedVisit.Recommendations[i].FullName + 
        " (" + this.selectedVisit.Recommendations[i].UserType + "):<br />";
    BPRecommendation += this.selectedVisit.Recommendations[i].Comments + 
        ".<br /><br />";
}
BPRecommendation += "</span>";

List<IElement> htmlarraylist = iTextSharp.text.html.simpleparser.HTMLWorker
    .ParseToList(new StringReader(BPRecommendation), null);

//add the collection to the document
for (int k = 0; k < htmlarraylist.Count; k++)
{
    paragraph.Add((IElement)htmlarraylist[k]);
}
doc.Add(paragraph);

【问题讨论】:

  • 哪一行导致异常?使用您的调试器找出 哪个 变量为空。
  • 是htmlarraylist。如果我从它起作用的文本中删除

标签: c# asp.net-mvc itextsharp telerik-mvc


【解决方案1】:

使用 HtmlEncoding。

if (i > 0) BPRecommendation += "<br />";
BPRecommendation += HttpUtility.HtmlEncode(this.selectedVisit.Recommendations[i].FullName) + 
    " (" + HttpUtility.HtmlEncode(this.selectedVisit.Recommendations[i].UserType) + "):<br />";
BPRecommendation += HttpUtility.HtmlEncode(this.selectedVisit.Recommendations[i].Comments) + 
    ".<br /><br />";

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-15
    • 2012-03-06
    • 2021-11-22
    • 2011-03-27
    相关资源
    最近更新 更多