What Iam doing wrong?

OK. What Iam doing wrong here
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.


Answer this question

What Iam doing wrong?

  • Nate Hekman

    Hmm, http://forums.asp.net/ might be a better place to ask this question.
  • AndrewRise

    can you post the value of HtmlFile and MenuBar after the calls to ReadHtmlFile() and GenerateMenuBar()  
    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

    HtmlFile is whole HTML file...

    Value of HtmlFile:

    <html>
    <head>
    <title>Willness Programs - ViewSonic / Willness [We like challanges] - wAutorun v1.0</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <link href="styles.css" rel="stylesheet" type="text/css">
    </head>

    <body>
    <table width="900"  border="0" cellpadding="0" cellspacing="0" id="Noseca tabela">
      <tr>
        <td><table width="100%"  border="0" cellspacing="0" cellpadding="0">
          <tr>
            <td height="70" colspan="2" valign="top"><img src="images/willness_logo1.jpg" alt="WillnessR Autorun

    Index Page" width="484" height="70" border="0" usemap="#Map"><img src="images/willness_logo2.jpg"

    width="416" height="70"></td>
            </tr>
          <tr>
            <td width="23%" height="25"><table width="100%"  border="0" cellspacing="1" cellpadding="1">
              <tr>
                <td height="25" align="center" bgcolor="#990000">&nbsp;</td>
              </tr>
            </table></td>
          <td width="77%"><table width="100%"  border="0" cellspacing="1" cellpadding="1">
            <tr>
              <td height="25" bgcolor="#CC0000" class="huge_text">Index Page</td>
            </tr>
          </table></td>
          </tr>
          <tr>
            <td valign="top" bgcolor="#CCCCCC">
    %menu_loop%
                  <table width="100%"  border="0" cellspacing="1" cellpadding="1">
            <tr>
              <td width='10%' height='20' align='center' valign='top'><b>&raquo;</b></td>
                       <td height="20" align="left" valign="top">%menu_bar%</td>
            </tr>
                  </table>
    %menu_loop%
    </td>
            <td valign="top" bgcolor="#EAEAEA"><table width="100%"  border="0" cellspacing="1" cellpadding="1">
              <tr>
                <td bgcolor="#EAEAEA">
                </td>
              </tr>
            </table></td>
          </tr>
          <tr>
            <td height="20" align="center"><table width="100%"  border="0" cellspacing="1" cellpadding="1">
              <tr>
                <td height="25" align="center" bgcolor="#990000"><span class="copyright_text"><a title="Visit autor's

    Home Page" href="http://www.willness.co.sr" target="_new" class="copyright_link">Willness&reg;</a> Copyright

    03/04</span></td>
              </tr>
            </table></td>
            <td><table width="100%"  border="0" cellspacing="1" cellpadding="1">
              <tr>
                <td height="25" align="center" bgcolor="#CC0000"><a title="About WillnessR"

    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></td>
              </tr>
            </table></td>
          </tr>
        </table></td>
      </tr>
    </table>
    <map name="Map">
      <area shape="rect" coords="0,0,145,68" href="index.html" alt="WillnessR Autorun Index Page">
    </map>
    </body>
    </html>


    Value of MenuBar is everything between two ocurenced of %menu_loop%:

                  <table width="100%"  border="0" cellspacing="1" cellpadding="1">
            <tr>
              <td width='10%' height='20' align='center' valign='top'><b>&raquo;</b></td>
                       <td height="20" align="left" valign="top">%menu_bar%</td>
            </tr>
                  </table>


    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

    No. This is Windows Application (exe), but manipulates with HTML file because Iam making a program for CD/DVD autorun.

  • SROSeaner

    I should have mention it, %menu_bar% is replaced inside GenerateMenuBar function.
    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

    Anyone
    I need opinion why this code doesen't work how it suppost to.
    Thank you.

  • DianaBurke

    so where do you do the replacement of %menu_bar%.  the code you have at the top only replaces %menu_loop%.  if you're ending up with combined href tags, then i would assume that whatever logic you have that replaces the %menu_bar% is what has the problem in it.


  • What Iam doing wrong?