Hello,
I am trying to read in an html file, and replace a custom tag with a string that will be generated from the application.
I am guessing the way to do this has changed with .NET 2.0, as the books I have refer to FileInfo objects that are not being accepted by Visual C# Express.
I would appreciate it if somebody could provide me with some sample code, or point me to some references on the web.
Thank you

reading/editing html file
sundanceca
Thanks for the reply Jon.
It will be a fairly small HTML file -- about 15KB
The application I am writing is a manager for ads on a website.
(I was going to do it in ASP.NET but there were issues)
Basically the application will iterate through each "Ad" in a Datagridview control, and write the appropriate HTML.
Here is what I currently have:
string sideBarHtml = "";
const string FILE_NAME = @"c:\final.html";
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if (row.Cells["Position"].Value.ToString() == "SideBar")
{
sideBarHtml += "<tr align=\"center\">\n<td>\n<a href = \"" +
row.Cells["Anchor"].Value.ToString() + "\"><img src = \"" +
row.Cells["Href"].Value.ToString() + "\" alt=\"" +
row.Cells["AlternateText"].Value.ToString() +
"\" height = \"" + row.Cells["Height"].Value.ToString() +
"\" width=\"" + row.Cells["Width"].Value.ToString() +
"\" border=\"0\"></a>";
}
}
FileInfo sourceFile = new FileInfo(@"C:\template.html");
StreamReader stream = sourceFile.OpenText();
string text;
string htmlHolder = "";
do
{
text = stream.ReadLine();
htmlHolder += text;
} while (text != null);
stream.Close();
string finalHtml = htmlHolder.Replace("<SIDEBAR>", sideBarHtml);
StreamWriter sr = File.CreateText(FILE_NAME);
sr.Write(finalHtml);
sr.Close();
wbPreview.DocumentText = finalHtml;
Sorry about the mess.
This gets by, but I am running into some problems.
1. All line breaks are ignored, and the final HTML file is a mess when viewing source.
2. I am sure there must be an easier or better way of doing this. I cant imagine this being the most efficient way.
Thanks for your help.
Adam
Alexxey
2) It would be simpler just to use StreamReader.ReadToEnd(). That would also be more efficient as you're using string concatenation at the moment which will end up copying a lot of memory.
Note that you should use "using" blocks for both the StreamReader and the StreamWriter, so that they still get closed even if there's an exception. (At that point, you don't need the Close calls, as when the object is disposed, it will close automatically.)
So, something like:
string htmlHolder;
using (StreamReader reader = new StreamReader(@"C:\template.html"))
{
htmlHolder = reader.ReadToEnd();
}
string finalHtml = htmlHolder.Replace("
using (StreamWriter writer = File.CreateText(FILE_NAME))
{
writer.Write (finalHtml);
}
wbPreview.DocumentText = finalHtml;
Mookey
Thank you for your reply.
Everything works great. Thank you very much for your help, and for explaning "why" I should be doing it as you stated. It helped to make sense of everything.
Thanks,
Adam
zoltak
i would still appreciate any advice on how to do this, though.
Thanks!
Benjamin Chebrou
Are the HTML files small enough to load entirely into memory (Using TextReader.ReadToEnd()).
Is the replacement a simple string one, or do you need to replace patterns
If you could give some examples of what you need to do, that would help.
Jon