Solved

Replacing the line in the string with file contents

Posted on 2014-01-08
2
193 Views
Last Modified: 2014-01-18
Hi,
I am trying to read a string(it has HTML content) in the .Net application.
 Whenever <!--#include file= is found (Ex: <!--#include file="../search/SearchInfo.aspx" -->) I will read the SearchInfo.aspx page contents and  replace  this line(<!--#include file="../search/SearchInfo.aspx") with the actual SearchInfo page contents.I have 5 to 6  <!--#include file in the string.

Please let me know the good and easy way to do this.

Thanks
0
Comment
Question by:KavyaVS
2 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 39767553
this code should do:
string data = string.Empty;
            using (System.IO.StreamReader sr = new StreamReader("c:\\temp\\yourfile.txt"))
            {
                data = sr.ReadToEnd();
                sr.Close();
            }

            Regex r = new Regex("[<][!][-][-]\\s*[#]include file[=]\"(.+)\"\\s*[-][-][>]");

            MatchCollection c = r.Matches(data);
            foreach (Match m in c)
            {
                string fc = m.Value;
                string fn = m.Groups[1].Value;

                try
                {
                    using (System.IO.StreamReader sr = new StreamReader(fn))
                    {
                        fc = sr.ReadToEnd();
                        sr.Close();
                    }
                }
                catch {}

                data = data.Replace(m.Value, fc);
            }

            using (System.IO.StreamWriter sw = new StreamWriter("c:\\temp\\resultfile.txt"))
            {
                sw.Write(data);
                sw.Close();
            }

Open in new window

0
 

Author Closing Comment

by:KavyaVS
ID: 39791303
Thanks
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question