I have wrote this function, but it doesen't work :S.
ReadHTMLFile() is a function that reads whole HTML file.
GenerateMenuBar() is a function to generate menu bar for the HTML file.
Then I do what I need to do (is in WriteHTMLFile() function) and when I write the file I get a page like some tags are missing (truncated :S).
I compare the html files (the original one and the generated one) and Ive notice that at the bottom of the file some tags are in very strange place (see below the code).
public void WriteHTMLFile() { string HtmlFile = ReadHTMLFile(); string MenuBar = GenerateMenuBar(); string FinalFile = ""; string temp; int i, j, k; FileStream fs = File.OpenWrite(@"C:\index.html"); StreamWriter wr = new StreamWriter(fs,Encoding.ASCII); temp = HtmlFile.Substring(0, HtmlFile.IndexOf("%menu_loop%")); i = temp.Length; temp = HtmlFile.Substring(0, HtmlFile.LastIndexOf("%menu_loop%")); j = temp.Length; k = j - i; temp = HtmlFile.Substring(HtmlFile.IndexOf("%menu_loop%"), k); HtmlFile = HtmlFile.Remove(HtmlFile.IndexOf("%menu_loop%") + 11, temp.Length); FinalFile = HtmlFile.Replace("%menu_loop%", MenuBar); wr.Write(FinalFile.ToString()); // fs.Close(); // wr.Close(); } |
See the bolded text:
<td height="25" align="center" bgcolor="#990000"><span class="copyright_text"><a title="Visit autor's Home Page" href="http://www.willness.cllnessR" href="about_willness.html" class="mini_text">About Willness</a> <span class="mini_text">|</span> <a title="Copyright Privilages" href="copyright_privilages.html" class="mini_text">Copyright Privilages</a>
It seems to me that two <a href tags are combine into each other :S.
Why is this happening Causing my HTML page to look ugly.
This is not all. When I have bigger HTML file that problem above isn't present, but it truncate the tags at the bottom.
Like that string variable have no more place to store the chars.
Please help me. Iam trying to solve this problem for about 2-3 hours, but no luck. Iam desperate.
Can you have any idea what might have happened here Thanks in advance.

What Iam doing wrong?
Nate Hekman
AndrewRise
From looking at your code...it appears you're trying to replace whatever's between the first occurence of %menu_loop% and the last, with the contents of MenuBar.
I would've written that sort of thing in a function like:
string
ReplaceInBetwen(string text, string searchText, string textReplacement){
string left = text.Substring(0, text.IndexOf(searchText)); string right = text.Substring(text.LastIndexOf(searchText) + searchText.Length); return left + textReplacement + right;}
And so your WriteHtmlFile would just look like:
void
WriteHtml(){
using (StreamWriter wr = new StreamWriter(@"c:\index.html")){
wr.Write(ReplaceInBetwen(ReadHtmlFile(),
"%menu_loop%", GenerateMenuBar()));wr.Close();
}
}
sluggy
Value of HtmlFile:
Value of MenuBar is everything between two ocurenced of %menu_loop%:
In this string I have %menu_bar% with I need to replace with <a href... of the each file.
I put %menu_loop% because I want to loop whatever is between %menu_loop% and when I replace links (<a href...) with data I want, it left only two occurences of %menu_loop%.
I remove one occurence and left one use for replacing for generated MenuBar.
This is my idea, but when I write to the html file, it gets truncated :S.
I hope you understand.
Thanks for the code. I will try it.
MumbaiKaChela
SROSeaner
I solved the problem with your help and because you provide your code.
You can see that I have comment wr.Close();. This is why my file gets truncated.
I have tested your code with and without the wr.Close(); and mine code to.
Maybe I have confused you, but main question was for truncating the file.
Replace stuff is OK before I open this thread.
Maybe I explain before, but let me explaing again.
%menu_bar% get replaced for each folder in selected root folder (that what returns GenerateMenuBar along with table (everything what's inside %menu_loop%)).
Then I remove one occurence of %menu_loop% and have another one for relpacing with GenerateMenuBar and all this write to an HTML file.
Thank you very much for your time and help.
Best Regards.
delder
I need opinion why this code doesen't work how it suppost to.
Thank you.
DianaBurke