Link to home
Start Free TrialLog in
Avatar of SasfinSD
SasfinSD

asked on

C# StreamWriter replace only Specified amount of Characters

Hi everyone

Further to my post about Streamreader/writer, I have 1 more question which I hope you can help with.

Here is the scenario:

Using Streareader, I'm reading a Template text file with HTML formatting.
I'm then Saving the file using StreamWriter as a HTM file.

I've placed Line Markers in the Original Template for example : #REPLACEME1 and I then replace the whole line in the output file with the correct string which includes the data from my Web form. For example :

string Company = ddCompany.Text + @"<o:p></o:p></span></b></p>";

The problem is that I lose some of my HTML formatting when replacing the WHOLE line.

What I want , is some way to only replace the word #REPLACEME1 with ddCompany.Text without replacing the whole line.

Below an example of my code
protected void Button1_Click(object sender, EventArgs e)
        {
            string Firstname = tbName.Text + @"</span></u></b><b><span lang=EN-GB style='font-size:12.0pt;font-family:""Arial"",""sans-serif"";color:#000066'></span></b><span lang=EN-GB style='font-size:12.0pt;font-family:""Arial"",""sans-serif"";color:#1F497D'><o:p></o:p></span></p>";
            string Jobtitle = tbJobTitle.Text + @"<span lang=EN-GB style='font-size:9.0pt;font-family:""Arial"",""sans-serif"";color:#000066'></span><span lang=EN-GB style='font-size:9.0pt;font-family:""Arial"",""sans-serif""'><o:p></o:p></span></p>";
 
            //FIRST READ FROM TEMPLATE FILE AND CREATE NEW FILE SO LONG BASED ON TEMPLATE    
            GenerateOutputfromTemplate(TemplateFile, Firstname);
 
            //NEXT CALL METHOD TO INPUT THE OTHER VALUES
            Writevaluestoutput(Jobtitle, "#REPLACEME2")
 
        }
 
 
        public void GenerateOutputfromTemplate(string InputFilePath, string Firstname)
        {
            using (StreamReader TemplateStream = new StreamReader(InputFilePath))
            {
                using (StreamWriter newHTMLfile = new StreamWriter(HTMLOutputfile))
                {
                    string content = TemplateStream.ReadToEnd();
                    string newName = content.Replace("#REPLACEME1", Firstname);
                    newHTMLfile.Write(newName);
                    newHTMLfile.Close();
 
                }
 
            }
        }
 
 
 
        public void Writevaluestoutput(string Objecttowrite, string Replacekey)
        {
            using (StreamReader ReadHTMLFile = new StreamReader(HTMLOutputfile))
            {
                string content = ReadHTMLFile.ReadToEnd();
 
                ReadHTMLFile.Close();
 
                using (StreamWriter newHTMLfile = new StreamWriter(HTMLOutputfile))
                {
 
                    string newObject = content.Replace(Replacekey, Objecttowrite);
                    newHTMLfile.Write(newObject);
 
                }
            }
        }

Open in new window

Avatar of Adam
Adam
Flag of United Kingdom of Great Britain and Northern Ireland image

Could you paste you template file too, so we can try this out?

Also, and not so relevant, why do you have Word HTML tags (<o:p>) and empty span and bold tags in your HTML? See the lines below - they could be used to replace your two lines with no change of formatting.
Even in this state, they raise some questions. Why are you inserting HTML end tags, but not the equivalent start tags (<span>, <u>, <b>, <p>) before the Firstname and Jobtitle.

You'de be better off making sure the tags balance in the template and just insert the data, rather than adding more HTML tags
string Firstname = tbName.Text + @"</span></u></b></p>";
string Jobtitle = tbJobTitle.Text + @"</p>";

Open in new window

I had a think about your entire example, and I've written a far simpler replacement.
To make it more usable, you probably want to extract some of the code into separate methods, but this is to give you the idea.
    protected void Button1_Click(object sender, EventArgs e)
    {
        string Firstname = tbName.Text;
        string Jobtitle = tbJobTitle.Text;
        string content = "";
 
        // Read the Template File
        using (StreamReader TemplateStream = new StreamReader(InputFilePath))
        {
            content = TemplateStream.ReadToEnd();
        }
 
        // Do all your replacements
        content = content.Replace("#REPLACEME1", Firstname);
        content = content.Replace("#REPLACEME2", Jobtitle);
 
        // Write the HTML file to its final destination
        using (StreamWriter newHTMLfile = new StreamWriter(HTMLOutputfile))
        {
            newHTMLfile.Write(content);
        }
    }

Open in new window

Avatar of SasfinSD
SasfinSD

ASKER

Hi Spy

The reason why the tags look all confusing is because they are inserted into the middle of a line of HTML code.

Also, I must admit that I don't know HTML markup at all.

The template is from a outlook HTML file.

Herewith a part of the template to explain to you how and where I insert into the HTML:


<p class=MsoNormal><b><u><span lang=EN-GB style='font-size:12.0pt;font-family:
"Arial","sans-serif";color:#000066'>#REPLACEME1 </span></u></b><span
lang=EN-GB style='font-size:12.0pt;font-family:"Arial","sans-serif";color:#1F497D'><o:p></o:p></span></p>
 
<p class=MsoNormal style='line-height:12.0pt;mso-line-height-rule:exactly'><span
lang=EN-GB style='font-size:9.0pt;font-family:"Arial","sans-serif";color:#000066'>#REPLACEME2&nbsp; </span><span lang=EN-GB style='font-size:9.0pt;font-family:
"Arial","sans-serif"'><o:p></o:p></span></p>
 
<p class=MsoNormal style='line-height:12.0pt;mso-line-height-rule:exactly'><span
lang=EN-GB style='font-size:9.0pt;font-family:"Arial","sans-serif";color:#000066'>#REPLACEME3&nbsp; <o:p></o:p></span></p>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Adam
Adam
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks for the help. I actually redid the template using a HTML generator and the problem is now sorted. Thank for yer help.
Only a B - where did I go wrong? You know giving an A doesn't cost you any points, and I think I gave you plenty of advice here, both regarding your HTML template, and a better method of replacing the text.