?
Solved

Replacing the line in the string with file contents

Posted on 2014-01-08
2
Medium Priority
?
201 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 1500 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

649 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