Solved

Replacing the line in the string with file contents

Posted on 2014-01-08
2
183 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
Comment Utility
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
Comment Utility
Thanks
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

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…
In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
This video discusses moving either the default database or any database to a new volume.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

771 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

11 Experts available now in Live!

Get 1:1 Help Now