Question

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();
    }

What could be the problem here.

EDIT:

Here is the sample:

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!!

Was it helpful?

Solution

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);

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

EDIT: 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.

OTHER TIPS

You can try another way like this :)

string foo = "First Line\nSecond Line\nThird Line";
foo = foo.Replace("\n", @"\" + "n");

The point is seperate \ and n(or r,t).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top