为什么当我读取 XML 文档时会得到 \r\r\n\n 等?
我知道这些是转义字符,但如何从 XML 文档中读取并忽略它们?顺便说一句,我正在使用 XmlDocument
。
I understand these are escape characters but how do I read from a XML document and ignore them? I'm using XmlDocument
, by the way.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您从文件中读取的字符串实际上并不包含
"\r\n"
。这些是转义序列。'\r'
和'\n'
分别表示回车符和换行符,它们一起构成换行符。但是,如果您使用 VS 调试器查看字符串,您可能会看到转义序列而不是实际的换行符。来自 MSDN:
示例:
输出:
如果您确实想删除字符串中的换行符,您可以使用正则表达式:
The string you read from the file does not literally contain
"\r\n"
. Those are escape sequences.'\r'
and'\n'
represent a carriage-return and new-line character, respectively, which form together a line break.If you're looking with the VS debugger at the string, however, you may see the escape sequences instead of actual line breaks. From MSDN:
Example:
Output:
If you actually want to get rid of line breaks in a string, you can, for example, use a regex:
答案取决于您从文本文件中读取的具体程度以及您最终想要完成的任务。
这是一个非常简单的解决方案:
The answer depends on how specifically you're reading from the text file and what you're trying to accomplish in the end.
Here's a very simple solution:
您可以对文件的内容执行 string.Replace 操作,如下所示:
You can do a string.Replace on the content of the file like so: