Solved

Replacing the line in the string with file contents

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

724 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