将文本附加到完整的html字符串

时间:2016-04-14 18:13:57

标签: c# html string text append

我有一个完整的HTML文档文本字符串 - 它以结束html标记结束。我需要在这个htmlText中附加文本和换行符。有时我需要在htmlText之前插入一行,有时候在。之后。

只做stringText +“'<'br />”是否正确+ htmlText +“'<'br />” + moreStringText?

到目前为止,我测试的内容看起来是正确的,但我不确定处理这个问题的正确方法是什么?

谢谢,

Mike T

3 个答案:

答案 0 :(得分:1)

您希望作为网页一部分呈现的任何文字都需要放在<html></html>标记内,以使其有效。

传递以下内容

<!doctype html><head><title>Html Page</title></head></html>

My text here

W3 HTML validator会产生以下错误:

Error: Non-space character in page trailer.
From line 3, column 1; to line 3, column 11
></html>↩↩My text here

此外,正如slnit所述,您应该使用StringBuilder而不是使用字符串连接。

您希望将哪些文字添加到HTML页面的开头或结尾?

答案 1 :(得分:0)

您可以使用SringBuilder类。

string body =&#34;一些文字&#34;;

        StringBuilder buildHtml = new StringBuilder();
        buildHtml.Append("<HTML>");
        buildHtml.AppendLine(body);
        buildHtml.Append("</HTML>");
        string sHtml = buildHtml.ToString();

答案 2 :(得分:0)

我找到了解决方案 - 使用DevEx RichEditDocumentServer接口,我正在使用SubDocument InsertHtml和InsertText方法,它工作得很好!