make the text file read in 1 line ... help
hi all~ i am new in this forum, i need help in C# from everyone else.
i wish to make the following text file read in one line, such as below:
before that:
</tr><tr class="one">
<td>108 Mile Ranch</td>
<td>ZMH</td>
<td><a href="http://www.world-airport-codes.com/Canada/108-mile-ranch-7865.html"><img src="airport_codes_files/info.gif" border="0"></a></td><td><a href="http://www.world-airport-codes.com/canada/108-mile-ranch-7865.html">108 Mile Ranch</a></td>
<td>Canada</td>
<td>CA</td>
<td>906</td>
</tr><tr>
andmake it to the following...
</tr><tr class="one"><td>108 Mile Ranch</td><td>ZMH</td><td><a href="http://www.world-airport-codes.com/Canada/108-mile-ranch7865.html"><img src="airport_codes_files/info.gif" border="0"></a></td><td><a href="http://www.world-airport-codes.com/canada/108-mile-ranch-7865.html">108 Mile Ranch</a></td>
<td>Canada</td><td>CA</td><td>906</td></tr><tr>
so that this is my coding as below.. is a simple read text file
using System;
using System.IO;
using System.Collections;
namespace TextFileReader_csharp
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
public class FileClass
{
public static void Main()
{
int counter = 0;
string line;
// Read the file and display it line by line.
System.IO.StreamReader file =
new System.IO.StreamReader("c:\\data.txt.html");
string mystring = file.ReadToEnd();
file.Close();
//Display the file contents.
Console.WriteLine(mystring);
// Suspend the screen.
Console.ReadLine();
}
}
}
}
so what coding should i include into the main?
thanks & regard...
[2086 byte] By [
jayzee] at [2007-11-20 11:20:25]

# 2 Re: make the text file read in 1 line ... help
Assuming you want it into a string variable, here's how you would do it:
using System.Text;
// ...
StringBuilder sb = new StringBuilder();
while (!file.EndOfStream)
{
sb.Append(file.ReadLine());
}
string html = sb.ToString();
The StringBuilder is a class that appends strings to a memory location, allocating more memory as needed. This is much more efficient than appending to a string, because then the whole string has to be read, concatenated and reallocated in memory.
Another solution is to remove the linebreaks from the string:
string html = file.ReadToEnd().Replace("\r", "").Replace("\n", "");
# 3 Re: make the text file read in 1 line ... help
thx anyway! so now if i want to seach for the "<tr class="one" >inside the text file then follow by make it in a new line such like: and new line for </tr><tr class="two">
</tr><tr class="one"><td>108 Mile Ranch</td><td>ZMH</td><td><a href="http://www.world-airport-codes.com/Canada/108-mile-ranch-7865.html"><img src="airport_codes_files/info.gif" border="0"></a></td><td><a href="http://www.world-airport-codes.com/canada/108-mile-ranch-7865.html">108 Mile Ranch</a></td><td>Canada</td><td>CA</td><td>906</td></tr><tr>
</tr><tr class="two"><td>Aachen</td><td>AAH</td><td><a href="http://www.world-airport-codes.com/Germany/aachen/merzbruck-7866.html"><img src="airport_codes_files/info.gif" border="0"></a></td><td><a href="http://www.world-airport-codes.com/germany/aachen/merzbruck7866.html">Aachen/Merzbruck</a></td><td>Germany</td><td>DE</td><td>623</td></tr><tr>
jayzee at 2007-11-9 11:38:35 >

# 4 Re: make the text file read in 1 line ... help
using System.Text;
// ...
StringBuilder sb = new StringBuilder();
while (!file.EndOfStream)
{
sb.Append(file.ReadLine());
}
sb.Replace("</tr><tr class=\"one\">", "\r\n</tr><tr class=\"one\">");
sb.Replace("</tr><tr class=\"two\">", "\r\n</tr><tr class=\"two\">");
string html = sb.ToString();
# 5 Re: make the text file read in 1 line ... help
thanks! this is my coding
mystring = mystring.Trim().Replace("\n", "").Replace("\r", "");
mystring = mystring.Trim().Replace("</tr><tr class=\"one\">", "\r\n</tr><tr class=\"one\">");
mystring = mystring.Trim().Replace("</tr><tr class=\"two\">", "\r\n</tr><tr class=\"two\">");
sw.Write(mystring);
sw.Flush();
but the result come out is
</tr><tr class="two"><td>Aachen</td><td>AAH</td><td><a href="http://www.world-airport-codes.com/Germany/aachen/merzbruck-7866.html"><img src="airport_codes_files/info.gif" border="0"></a></td><td><a href="http://www.world-airport-codes.com/germany/aachen/merzbruck-7866.html">Aachen/Merzbruck</a></td><td>Germany</td><td>DE</td><td>623</td></tr><tr></tr><tr class="one"><td>Aachen</td><td>ZIU</td><td><a href="http://www.world-airport-codes.com/Germany/railway-7867.html"><img src="airport_codes_files/info.gif" border="0"></a></td><td><a href="http://www.world-airport-codes.com/germany/railway-7867.html">Railway</a></td><td>Germany</td><td>DE</td><td>429</td></tr><tr>
why i cant separate the class two and class one in new line?
jayzee at 2007-11-9 11:40:41 >

# 7 Re: make the text file read in 1 line ... help
Using \r\n for a line break is not really a good solution. If someone want to use your application with MONO or under FreeBSD that will break the portability. It is better to use the Environment.NewLine functionality.
mystring = mystring.Trim().Replace("\n", "").Replace("\r", "");
mystring = mystring.Trim().Replace("</tr><tr class=\"one\">", Environment.NewLine+"</tr><tr class=\"one\">");
mystring = mystring.Trim().Replace("</tr><tr class=\"two\">", Environment.NewLine+"</tr><tr class=\"two\">");
torrud at 2007-11-9 11:42:34 >
