user
user

Reputation: 128

Find and Replace text in html file

I am working with Winforms in C#. i have following Html File.

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta name="generator" content="HTML Tidy for Windows (vers 25 March 2009), see www.w3.org" />
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii" />
<link rel="stylesheet" type="text/css" href="styles.css" />
<title></title>
</head>
<body>
<p><b>Chapter 1</b></p>
</body>
</html>

And i want to replace <link rel="stylesheet" type="text/css" href="styles.css" /> with "<link rel="stylesheet" type="text/css" href=""+ htmlFile +"" />" I have tried the following code but it doesnt work

 string outpageFile = File.ReadAllText(StaticClass.outpage);
            string htmlFile= StaticClass.ZipFilePath + "\\OEBPS\\styles.css";
            outpageFile = outpageFile.Replace("<link rel='stylesheet' type='text/css' href='styles.css' />", "<link rel='stylesheet' type='text/css' href='"+ htmlFile +"' />");
            File.WriteAllText(StaticClass.outpage, outpageFile);

But it din't work. Main Problem is occuring in double inverted commas that are being used. How to do it then ??

Upvotes: 0

Views: 9563

Answers (2)

smilu
smilu

Reputation: 899

What I understand is you will have a dynamic HTML page url in the LINK. Why you should try to change the entire link.

Try this. In your HTML make the LINK something like below which will be unique in your page and replace that particular string alone with your new LINK.

<link rel="stylesheet" type="text/css" href='@MYLINK' />

In application

filenamestring.replace("@MYLINK","http://www.google.com");

This should work perfectly

Upvotes: 1

Ondrej Svejdar
Ondrej Svejdar

Reputation: 22054

Use HtmlAgility pack (http://htmlagilitypack.codeplex.com/) - regex isn't the best way how to deal with replacements in html.

Example:

      string markup = @"<!DOCTYPE html PUBLIC ""-//W3C//DTD XHTML 1.0 Transitional//EN""
    ""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"">
<html xmlns=""http://www.w3.org/1999/xhtml"">
<head>
<meta name=""generator"" content=""HTML Tidy for Windows (vers 25 March 2009), see www.w3.org"" />
<meta http-equiv=""Content-Type"" content=""text/html; charset=us-ascii"" />
<link rel=""stylesheet"" type=""text/css"" href=""styles.css"" />
<title></title>
</head>
<body>
<p><b>Chapter 1</b></p>
</body>
</html>";
      var html = new HtmlAgilityPack.HtmlDocument();
      html.LoadHtml(markup);
      var links = html.DocumentNode.SelectNodes("//link");
      foreach (var link in links) {
        link.Attributes["href"].Value = StaticClass.ZipFilePath +
          "\\OEBPS\\styles.css";
      }

      var builder = new StringBuilder();
      using (var writer = new StringWriter(builder)) {
        html.Save(writer);
      }
      markup = builder.ToString();

Upvotes: 0

Related Questions