从 XML 文件 C# 中删除回车 [英] Remove carriage return from XML file C#

查看:65
本文介绍了从 XML 文件 C# 中删除回车的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从 XML 文件中删除回车,但我得到的只是一个带有一个框的 XML 文件(\r).我是这样写的:

I need to remove carriage return from an XML file, but what i get is only an XML file with one box(\r). I wrote this:

    string sourceFileName = textBox1.Text;

                     StreamReader sr = new StreamReader(sourceFileName);

                     String line;
                     String newLine = null;
                     // Read and display lines from the file until the end of 
                     // the file is reached.
                     while ((line = sr.ReadLine()) != null)
                     {

                         //Regex regex = new Regex(@"(\r\n|\r|\n)+");

                         newLine = line.Replace("\r\n", String.Empty);
//also tried line.Replace("\n", String.Empty).Replace("\r",String.Empty); , but no use

                         StreamWriter sw = new StreamWriter(@"C:\Users\manish\Desktop\output.xml");
                         sw.WriteLine(newLine);
                         sw.Close();
    }

这里可能有什么问题.

示例如下:

3C 3F 78 6D 6C 20 76 65  72 73 69 6F 6E 3D 27 31
30 27 3F 3E 3C 72 6F 6F  74 3E 20 E0 A8 8D E2 80
80 E3 B0 80 E4 8C 80 E4  A4 80 E4 90 80 E3 B8 80
E7 8C 80 E7 8C 80 E5 BC  80 E6 98 80 E6 B0 80 E3
88 80 E7 94 80 E3 8C 80  E5 BC 80 E6 B8 80 E6 88
80 E5 BC 80 E7 94 80 E3  8C 80 E5 BC 80 E6 8C 80
E3 84 80 E3 80 80 E5 BC  80 E7 8C 80 E3 84 80 E3
B0 80 E2 BC 80 E4 8C 80  E4 A4 80 E4 90 80 E3 B8
80 E2 80 80 E0 B4 80 E0  B4 80 0A 20 3C 62 6F 6F
6B 74 69 74 6C 65 3E 3C  21 5B 43 44 41 54 41 5B
5D 5D 3E 3C 2F 62 6F 6F  6B 74 69 74 6C 65 3E 3C
70 61 6E 65 6C 73 3E E0  A8 8D E3 B0 80 E7 80 80

<?xml version='10'?><root> à¨â€€ã°€äŒ€ä¤€ä€ã¸€çŒ€çŒ€å¼€æ˜€æ°€ãˆ€ç"€ãŒ€å¼€æ¸€æˆ€å¼€ç"€ãŒ€å¼€æŒ€ã„€ã€€å¼€çŒ€ã„€ã°€â¼€äŒ€ä¤€ä€ã¸€â€€à´€à´€
 <booktitle><![CDATA[]]></booktitle><panels>à¨ã°€ç€€æ„€æ¸€æ"€æ°€â€€ç€æ¤€ç€æ°€æ"€ã´€âœ€äœ€ä"€ä¼€äœ€åˆ€ä„€å€€ä €å¤€â€€åŒ€ä¬€ä¤€ä°€ä°€äˆ€å"€ä¤€ä°€ä€ä"€åˆ€âœ€ã¸€à´€à´€
<questionsAndAnswers>à¨ã°€æ €æ"€æ„€æ€æ¤€æ¸€æœ€ã¸€ã°€â„€å¬€äŒ€ä€ä„€å€ä„€å¬€ã°€æˆ€ã¸€ä¤€æ¸€ç€æ"€çˆ€ç€€çˆ€æ"€ç€æ¤€æ¸€æœ€â€€ä´€æ„€ç€€çŒ€ã°€â¼€æˆ€ã¸€å´€å´€ã¸€ã°€â¼€æ €æ"€æ„€æ€æ¤€æ¸€æœ€ã¸€à´€à´€
<question><![CDATA[<b>1 Location</b> Why is Arabia's location a good one for trade?]]></question>à¨ã°€æ„€æ¸€çŒ€çœ€æ"€çˆ€â¼€ã¸€à´€à´€
<ansCharCount>350</ansCharCount>à¨ã°€â¼€ç„€ç"€æ"€çŒ€ç€æ¤€æ¼€æ¸€çŒ€ä„€æ¸€æ€ä„€æ¸€çŒ€çœ€æ"€çˆ€çŒ€ã¸€à´€à´€
<questionsAndAnswers>à¨ã°€æ €æ"€æ„€æ€æ¤€æ¸€æœ€ã¸€ã°€â„€å¬€äŒ€ä€ä„€å€ä„€å¬€ã°€æˆ€ã¸€ä¤€æ¸€ç€æ"€çˆ€ç€€çˆ€æ"€ç€æ¤€æ¸€æœ€â€€ä´€æ„€ç€€çŒ€ã°€â¼€æˆ€ã¸€å´€å´€ã¸€ã°€â¼€æ €æ"€æ„€æ€æ¤€æ¸€æœ€ã¸€à´€à´€
<question><![CDATA[<b>2 Movement</b> Why was the location of Mecca ideal for the spread of ideas?]]></question>à¨ã°€æ„€æ¸€çŒ€çœ€æ"€çˆ€â¼€ã¸€à´€à´€
<ansCharCount>350</ansCharCount>à¨ã°€â¼€ç„€ç"€æ"€çŒ€ç€æ¤€æ¼€æ¸€çŒ€ä„€æ¸€æ€ä„€æ¸€çŒ€çœ€æ"€çˆ€çŒ€ã¸€à´€à´€
</panel>à¨ã°€ç€€æ„€æ¸€æ"€æ°€â€€ç€æ¤€ç€æ°€æ"€ã´€âœ€ä´€æ„€æ¤€æ¸€â€€ä¤€æ€æ"€æ„€âœ€ã¸€à´€à´€
<questionsAndAnswers>à¨ã°€æ €æ"€æ„€æ€æ¤€æ¸€æœ€ã¸€ã°€â„€å¬€äŒ€ä€ä„€å€ä„€å¬€å´€å´€ã¸€ã°€â¼€æ €æ"€æ„€æ€æ¤€æ¸€æœ€ã¸€à´€à´€
<question><![CDATA[<b>A Analyzing Primary Sources </b>What kind of teaching does the phrase "the use of the pen†refer to?]]></question>à¨ã°€æ„€æ¸€çŒ€çœ€æ"€çˆ€â¼€ã¸€à´€à´€
<ansCharCount>350</ansCharCount>à¨ã°€â¼€ç„€ç"€æ"€çŒ€ç€æ¤€æ¼€æ¸€çŒ€ä„€æ¸€æ€ä„€æ¸€çŒ€çœ

看起来很吓人..哈!!

Looks scary..huh!!

推荐答案

您正在逐行读取文件,因此返回的行将从不包含 \r, \n\r\n - 因为它们是行分隔符.

You're reading the file line by line, so the line returned will never contain \r, \n or \r\n - as those are line separators.

最简单的方法是将整个批次加载到内存中,删除所有换行符,然后将其写出:

The simplest approach is just to load the whole lot into memory, remove all the line breaks, then write it out:

string text = File.ReadAllText(sourceFileName);
text = text.Replace("\r", "").Replace("\n", "");
File.WriteAllText(sourceFileName, text);

当然,如果你想去掉回车,就去掉上面代码中的.Replace("\n", "").

Of course if you only want to remove carriage returns, just remove the .Replace("\n", "") in the above code.

现在我们可以看到您的文件,没有 XML 版本 10,这表明该文档在您获得它之前就已损坏.您应该在这一点上停止并向后返回源,直到获得有效的 XML 文件.继续你目前拥有的东西是没有意义的.

Now that we can see your file, there is no XML version 10, which suggests that the document is corrupt before you even get it. You should stop at this point and work backwards towards the source until you can get a valid XML file. There's no point in continuing with what you've currently got.

这篇关于从 XML 文件 C# 中删除回车的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆