我使用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\"> </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\"> </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'\">      </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'\">      </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'\">      </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'\">       </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'\">      </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'\">      </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'\">        </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'\">      </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'\">        </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'\">       </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\"> </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
你可以看到它有一大堆= \“我需要删除所有的\并用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\"> </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\"> </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'\">      </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'\">      </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'\">      </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'\">       </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'\">      </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'\">      </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'\">        </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'\">      </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'\">        </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'\">       </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\"> </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
答案 0 :(得分:1)
那些实际上并不在字符串中。
调试器显示字符串的字符串文字,包括转义字符。
要查看字符串的原始值,请使用可视化工具(左侧的按钮)
答案 1 :(得分:1)
\
实际上并不存在。只是显示器显示"
不是字符串内容的结尾
答案 2 :(得分:1)
然而,当我将鼠标悬停在文本上或为文本添加监视时,整个字符串仍会显示所有的字符串。我在这里做错了什么?
这只是调试器如何显示值的问题。实际文本是:
<html><head><meta http-equiv="Content-Type"
但是调试器使用\"
答案 3 :(得分:1)
原因是因为它显示了该字符串的字符串值表示...在字符串中,要显示双引号字符,首先必须使用\ char转义它。如果将其输出到控制台,则不应该看到转义符