0

我正在使用 Aspose.Words 将 Word 文档转换为 HTML,最后转换为包含文档所有 HTML 的字符串。

我遇到的麻烦是更换"\".

这是我从文档中得到的字符串:

htmlString  "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" /><meta http-equiv=\"Content-Style-Type\" content=\"text/css\" /><meta name=\"generator\" content=\"Aspose.Words for .NET 13.7.0.0\" /><title></title></head><body><div><p style=\"font-size:12pt; line-height:115%; margin:0pt 0pt 10pt\"><span style=\"color:#ff0000; font-family:Calibri; font-size:12pt; font-weight:bold\">Evaluation Only. Created with Aspose.Words. Copyright 2003-2011 Aspose Pty Ltd.</span></p><h2 style=\"font-size:13pt; line-height:115%; margin:10pt 0pt 0pt; page-break-after:avoid; page-break-inside:avoid\"><span style=\"color:#4f81bd; font-family:Cambria; font-size:13pt; font-weight:bold\">This is a header</span></h2><p style=\"font-size:11pt; line-height:115%; margin:0pt 0pt 10pt\"><span style=\"font-family:Calibri; font-size:11pt\">&#xa0;</span></p><p style=\"font-size:11pt; line-height:115%; margin:0pt 0pt 10pt\"><span style=\"font-family:Calibri; font-size:11pt\">Look at my awesome bulleted list.</span></p><p style=\"font-size:11pt; line-height:115%; margin:0pt 0pt 10pt\"><span style=\"font-family:Calibri; font-size:11pt\">&#xa0;</span></p><p style=\"font-size:11pt; line-height:115%; margin:0pt 0pt 0pt 18pt; text-indent:-18pt\"><span style=\"font-family:Calibri; font-size:11pt\">1)</span><span style=\"font:7.0pt 'Times New Roman'\">&#xa0;&#xa0;&#xa0;&#xa0;&#xa0; </span><span style=\"font-family:Calibri; font-size:11pt\">Option 1</span></p><p style=\"font-size:11pt; line-height:115%; margin:0pt 0pt 0pt 36pt; text-indent:-18pt\"><span style=\"font-family:Calibri; font-size:11pt\">a)</span><span style=\"font:7.0pt 'Times New Roman'\">&#xa0;&#xa0;&#xa0;&#xa0;&#xa0; </span><span style=\"font-family:Calibri; font-size:11pt\">Sup Option A</span></p><p style=\"font-size:11pt; line-height:115%; margin:0pt 0pt 0pt 36pt; text-indent:-18pt\"><span style=\"font-family:Calibri; font-size:11pt\">b)</span><span style=\"font:7.0pt 'Times New Roman'\">&#xa0;&#xa0;&#xa0;&#xa0;&#xa0; </span><span style=\"font-family:Calibri; font-size:11pt\">Sub Option B</span></p><p style=\"font-size:11pt; line-height:115%; margin:0pt 0pt 0pt 36pt; text-indent:-18pt\"><span style=\"font-family:Calibri; font-size:11pt\">c)</span><span style=\"font:7.0pt 'Times New Roman'\">&#xa0;&#xa0;&#xa0;&#xa0;&#xa0;&#xa0; </span><span style=\"font-family:Calibri; font-size:11pt\">Sub Option C</span></p><p style=\"font-size:11pt; line-height:115%; margin:0pt 0pt 0pt 18pt; text-indent:-18pt\"><span style=\"font-family:Calibri; font-size:11pt\">2)</span><span style=\"font:7.0pt 'Times New Roman'\">&#xa0;&#xa0;&#xa0;&#xa0;&#xa0; </span><span style=\"font-family:Calibri; font-size:11pt\">Option 2</span></p><p style=\"font-size:11pt; line-height:115%; margin:0pt 0pt 0pt 36pt; text-indent:-18pt\"><span style=\"font-family:Calibri; font-size:11pt\">a)</span><span style=\"font:7.0pt 'Times New Roman'\">&#xa0;&#xa0;&#xa0;&#xa0;&#xa0; </span><span style=\"font-family:Calibri; font-size:11pt\">Sub Option A</span></p><p style=\"font-size:11pt; line-height:115%; margin:0pt 0pt 0pt 54pt; text-indent:-18pt\"><span style=\"font-family:Calibri; font-size:11pt\">i)</span><span style=\"font:7.0pt 'Times New Roman'\">&#xa0;&#xa0;&#xa0;&#xa0;&#xa0;&#xa0;&#xa0; </span><span style=\"font-family:Calibri; font-size:11pt\">Sub </span><span style=\"font-family:Calibri; font-size:11pt\">Sub</span><span style=\"font-family:Calibri; font-size:11pt\"> Option </span><span style=\"font-family:Calibri; font-size:11pt\">i</span></p><p style=\"font-size:11pt; line-height:115%; margin:0pt 0pt 0pt 36pt; text-indent:-18pt\"><span style=\"font-family:Calibri; font-size:11pt\">b)</span><span style=\"font:7.0pt 'Times New Roman'\">&#xa0;&#xa0;&#xa0;&#xa0;&#xa0; </span><span style=\"font-family:Calibri; font-size:11pt\">Sub Option B</span></p><p style=\"font-size:11pt; line-height:115%; margin:0pt 0pt 0pt 54pt; text-indent:-18pt\"><span style=\"font-family:Calibri; font-size:11pt\">i)</span><span style=\"font:7.0pt 'Times New Roman'\">&#xa0;&#xa0;&#xa0;&#xa0;&#xa0;&#xa0;&#xa0; </span><span style=\"font-family:Calibri; font-size:11pt\">Sub </span><span style=\"font-family:Calibri; font-size:11pt\">Sub</span><span style=\"font-family:Calibri; font-size:11pt\"> Option </span><span style=\"font-family:Calibri; font-size:11pt\">i</span></p><p style=\"font-size:11pt; line-height:115%; margin:0pt 0pt 10pt 54pt; text-indent:-18pt\"><span style=\"font-family:Calibri; font-size:11pt\">ii)</span><span style=\"font:7.0pt 'Times New Roman'\">&#xa0;&#xa0;&#xa0;&#xa0;&#xa0;&#xa0; </span><span style=\"font-family:Calibri; font-size:11pt\">Sub </span><span style=\"font-family:Calibri; font-size:11pt\">Sub</span><span style=\"font-family:Calibri; font-size:11pt\"> Option ii</span></p><p style=\"font-size:11pt; line-height:115%; margin:0pt 0pt 10pt\"><span style=\"font-family:Calibri; font-size:11pt\">&#xa0;</span></p><p style=\"border-bottom-color:#4f81bd; border-bottom-style:solid; border-bottom-width:0.5pt; font-size:11pt; line-height:115%; margin:10pt 46.8pt 14pt; padding-bottom:4pt\"><span style=\"color:#4f81bd; font-family:Calibri; font-size:11pt; font-style:italic; font-weight:bold\">This is another heading</span></p></div></body></html>"   string

如您所见,它有一大堆 =\" 我需要删除所有 \'s 并将其替换为 String.Empty。

这是我正在使用的代码:

string htmlString = doc.ToString(SaveFormat.Html);
string text = htmlString.ToString().Replace(@"\", string.Empty);

但是,当我将鼠标悬停在整个字符串上text或添加手表时,仍会显示所有. 我在这里做错了什么?text\'s

这是文本的监视值:

text    "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" /><meta http-equiv=\"Content-Style-Type\" content=\"text/css\" /><meta name=\"generator\" content=\"Aspose.Words for .NET 13.7.0.0\" /><title></title></head><body><div><p style=\"font-size:12pt; line-height:115%; margin:0pt 0pt 10pt\"><span style=\"color:#ff0000; font-family:Calibri; font-size:12pt; font-weight:bold\">Evaluation Only. Created with Aspose.Words. Copyright 2003-2011 Aspose Pty Ltd.</span></p><h2 style=\"font-size:13pt; line-height:115%; margin:10pt 0pt 0pt; page-break-after:avoid; page-break-inside:avoid\"><span style=\"color:#4f81bd; font-family:Cambria; font-size:13pt; font-weight:bold\">This is a header</span></h2><p style=\"font-size:11pt; line-height:115%; margin:0pt 0pt 10pt\"><span style=\"font-family:Calibri; font-size:11pt\">&#xa0;</span></p><p style=\"font-size:11pt; line-height:115%; margin:0pt 0pt 10pt\"><span style=\"font-family:Calibri; font-size:11pt\">Look at my awesome bulleted list.</span></p><p style=\"font-size:11pt; line-height:115%; margin:0pt 0pt 10pt\"><span style=\"font-family:Calibri; font-size:11pt\">&#xa0;</span></p><p style=\"font-size:11pt; line-height:115%; margin:0pt 0pt 0pt 18pt; text-indent:-18pt\"><span style=\"font-family:Calibri; font-size:11pt\">1)</span><span style=\"font:7.0pt 'Times New Roman'\">&#xa0;&#xa0;&#xa0;&#xa0;&#xa0; </span><span style=\"font-family:Calibri; font-size:11pt\">Option 1</span></p><p style=\"font-size:11pt; line-height:115%; margin:0pt 0pt 0pt 36pt; text-indent:-18pt\"><span style=\"font-family:Calibri; font-size:11pt\">a)</span><span style=\"font:7.0pt 'Times New Roman'\">&#xa0;&#xa0;&#xa0;&#xa0;&#xa0; </span><span style=\"font-family:Calibri; font-size:11pt\">Sup Option A</span></p><p style=\"font-size:11pt; line-height:115%; margin:0pt 0pt 0pt 36pt; text-indent:-18pt\"><span style=\"font-family:Calibri; font-size:11pt\">b)</span><span style=\"font:7.0pt 'Times New Roman'\">&#xa0;&#xa0;&#xa0;&#xa0;&#xa0; </span><span style=\"font-family:Calibri; font-size:11pt\">Sub Option B</span></p><p style=\"font-size:11pt; line-height:115%; margin:0pt 0pt 0pt 36pt; text-indent:-18pt\"><span style=\"font-family:Calibri; font-size:11pt\">c)</span><span style=\"font:7.0pt 'Times New Roman'\">&#xa0;&#xa0;&#xa0;&#xa0;&#xa0;&#xa0; </span><span style=\"font-family:Calibri; font-size:11pt\">Sub Option C</span></p><p style=\"font-size:11pt; line-height:115%; margin:0pt 0pt 0pt 18pt; text-indent:-18pt\"><span style=\"font-family:Calibri; font-size:11pt\">2)</span><span style=\"font:7.0pt 'Times New Roman'\">&#xa0;&#xa0;&#xa0;&#xa0;&#xa0; </span><span style=\"font-family:Calibri; font-size:11pt\">Option 2</span></p><p style=\"font-size:11pt; line-height:115%; margin:0pt 0pt 0pt 36pt; text-indent:-18pt\"><span style=\"font-family:Calibri; font-size:11pt\">a)</span><span style=\"font:7.0pt 'Times New Roman'\">&#xa0;&#xa0;&#xa0;&#xa0;&#xa0; </span><span style=\"font-family:Calibri; font-size:11pt\">Sub Option A</span></p><p style=\"font-size:11pt; line-height:115%; margin:0pt 0pt 0pt 54pt; text-indent:-18pt\"><span style=\"font-family:Calibri; font-size:11pt\">i)</span><span style=\"font:7.0pt 'Times New Roman'\">&#xa0;&#xa0;&#xa0;&#xa0;&#xa0;&#xa0;&#xa0; </span><span style=\"font-family:Calibri; font-size:11pt\">Sub </span><span style=\"font-family:Calibri; font-size:11pt\">Sub</span><span style=\"font-family:Calibri; font-size:11pt\"> Option </span><span style=\"font-family:Calibri; font-size:11pt\">i</span></p><p style=\"font-size:11pt; line-height:115%; margin:0pt 0pt 0pt 36pt; text-indent:-18pt\"><span style=\"font-family:Calibri; font-size:11pt\">b)</span><span style=\"font:7.0pt 'Times New Roman'\">&#xa0;&#xa0;&#xa0;&#xa0;&#xa0; </span><span style=\"font-family:Calibri; font-size:11pt\">Sub Option B</span></p><p style=\"font-size:11pt; line-height:115%; margin:0pt 0pt 0pt 54pt; text-indent:-18pt\"><span style=\"font-family:Calibri; font-size:11pt\">i)</span><span style=\"font:7.0pt 'Times New Roman'\">&#xa0;&#xa0;&#xa0;&#xa0;&#xa0;&#xa0;&#xa0; </span><span style=\"font-family:Calibri; font-size:11pt\">Sub </span><span style=\"font-family:Calibri; font-size:11pt\">Sub</span><span style=\"font-family:Calibri; font-size:11pt\"> Option </span><span style=\"font-family:Calibri; font-size:11pt\">i</span></p><p style=\"font-size:11pt; line-height:115%; margin:0pt 0pt 10pt 54pt; text-indent:-18pt\"><span style=\"font-family:Calibri; font-size:11pt\">ii)</span><span style=\"font:7.0pt 'Times New Roman'\">&#xa0;&#xa0;&#xa0;&#xa0;&#xa0;&#xa0; </span><span style=\"font-family:Calibri; font-size:11pt\">Sub </span><span style=\"font-family:Calibri; font-size:11pt\">Sub</span><span style=\"font-family:Calibri; font-size:11pt\"> Option ii</span></p><p style=\"font-size:11pt; line-height:115%; margin:0pt 0pt 10pt\"><span style=\"font-family:Calibri; font-size:11pt\">&#xa0;</span></p><p style=\"border-bottom-color:#4f81bd; border-bottom-style:solid; border-bottom-width:0.5pt; font-size:11pt; line-height:115%; margin:10pt 46.8pt 14pt; padding-bottom:4pt\"><span style=\"color:#4f81bd; font-family:Calibri; font-size:11pt; font-style:italic; font-weight:bold\">This is another heading</span></p></div></body></html>"   string
4

4 回答 4

1

这些实际上不在字符串中。

调试器显示字符串的字符串文字,包括转义字符。

要查看字符串的原始值,请使用可视化工具(左侧的按钮)

于 2013-08-26T20:38:49.957 回答
1

\s 实际上并不存在。它只是向您显示"不是字符串内容的结尾的显示

于 2013-08-26T20:38:50.493 回答
1

但是,当我将鼠标悬停在文本上或添加文本监视时,整个字符串仍会显示所有 \'s。我在这里做错了什么?

这只是调试器如何显示值的问题。实际的文字是:

<html><head><meta http-equiv="Content-Type"

但是调试器使用\"

于 2013-08-26T20:39:05.240 回答
1

原因是因为它向您显示了该字符串的字符串值表示...在字符串中,要显示双引号字符,您首先必须使用 \ 字符对其进行转义。如果将其输出到控制台,则不应看到转义 ch

于 2013-08-26T20:39:39.570 回答