Solved

Replacing the line in the string with file contents

Posted on 2014-01-08
2
186 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 142

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Duplicate a row 2 46
Rolling back a sequence of stored procedures 12 38
SQL Exceptions 3 37
Pagebreak issue while printing the aspx page 3 14
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…
I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

912 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now