Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Modify HTML file in a c sharp class

Tags:

html

c#

First of all my application is not a web application. My aim is reading html files and modify them.

So that I write this code:

    string fileName = "~\\ActivationMail.html";
    string textLine = string.Empty;

    System.IO.StreamReader objReader;
    objReader = new System.IO.StreamReader(fileName);

    do
    {
        textLine += objReader.ReadLine();
        if (textLine.Contains("id=\"link\""))
        {
            textLine += "href=\"www.google.com\"";
        }
    } while (objReader.Peek() != -1);

    objReader.Close();

But each time reading whole html file is not seems to best solution. Can I change specific divs without reading(looping) whole html file? Is it possible?

like image 805
Eren Avatar asked Dec 31 '25 09:12

Eren


1 Answers

If you are wanting to just change certain things, you could be able to do this with XPath. Using the HTML Agility Pack, you can do this using C#.

http://htmlagilitypack.codeplex.com/

like image 148
Arran Avatar answered Jan 01 '26 23:01

Arran



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!