为什么当我读取 XML 文档时会得到 \r\r\n\n 等?

发布于 2024-11-07 08:07:26 字数 75 浏览 1 评论 0原文

我知道这些是转义字符,但如何从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夜访吸血鬼 2024-11-14 08:07:26

您从文件中读取的字符串实际上并不包含 "\r\n"。这些是转义序列'\r''\n' 分别表示回车符和换行符,它们一起构成换行符。

但是,如果您使用 VS 调试器查看字符串,您可能会看到转义序列而不是实际的换行符。来自 MSDN

注意
在编译时,逐字字符串将转换为具有所有相同转义序列的普通字符串。因此,如果您在调试器监视窗口中查看逐字字符串,您将看到编译器添加的转义字符,而不是源代码中的逐字版本。例如,逐字字符串 @"C:\files.txt" 将在监视窗口中显示为 "C:\\files.txt"

示例:

var mystring = "Hello\r\nWorld";

Console.Write(mystring);

输出:

Hello
World

如果您确实想删除字符串中的换行符,您可以使用正则表达式:

var result = Regex.Replace(mystring, @"\s+", " ");

// result == "Hello World";

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:

Note
At compile time, verbatim strings are converted to ordinary strings with all the same escape sequences. Therefore, if you view a verbatim string in the debugger watch window, you will see the escape characters that were added by the compiler, not the verbatim version from your source code. For example, the verbatim string @"C:\files.txt" will appear in the watch window as "C:\\files.txt".

Example:

var mystring = "Hello\r\nWorld";

Console.Write(mystring);

Output:

Hello
World

If you actually want to get rid of line breaks in a string, you can, for example, use a regex:

var result = Regex.Replace(mystring, @"\s+", " ");

// result == "Hello World";
娇纵 2024-11-14 08:07:26

答案取决于您从文本文件中读取的具体程度以及您最终想要完成的任务。

这是一个非常简单的解决方案:

StringBuilder sb = new StringBuilder();
using(var sr = new System.IO.StreamReader('path/to/your/file.txt'))
{
  while(true)
  {
     string line = sr.ReadLine();
     // if this is the final line, break out of the while look
     if(line == null)
        break;
     // append this line to the string builder
     sb.Append(line);
  }
  sr.Close();
}

// the sb instance hold all the text in the file, less the \r and \n characters
string textWithoutEndOfLineCharacters = sb.ToString();

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:

StringBuilder sb = new StringBuilder();
using(var sr = new System.IO.StreamReader('path/to/your/file.txt'))
{
  while(true)
  {
     string line = sr.ReadLine();
     // if this is the final line, break out of the while look
     if(line == null)
        break;
     // append this line to the string builder
     sb.Append(line);
  }
  sr.Close();
}

// the sb instance hold all the text in the file, less the \r and \n characters
string textWithoutEndOfLineCharacters = sb.ToString();
就此别过 2024-11-14 08:07:26

您可以对文件的内容执行 string.Replace 操作,如下所示:

string contents = File.ReadAllText('myfile.txt');
contents = contents.Replace('\n', '');

You can do a string.Replace on the content of the file like so:

string contents = File.ReadAllText('myfile.txt');
contents = contents.Replace('\n', '');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文