在C Sharp类中修改HTML文件 [英] Modify HTML file in a c sharp class

查看:100
本文介绍了在C Sharp类中修改HTML文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我的应用程序不是Web应用程序. 我的目的是读取html文件并对其进行修改.

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

以便我编写以下代码:

    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();

但是每次阅读整个html文件似乎都不是最好的解决方案.我可以更改特定的div而不读取(循环)整个html文件吗?有可能吗?

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?

推荐答案

如果只想更改某些内容,则可以使用XPath做到这一点.使用HTML Agility Pack,您可以使用C#做到这一点.

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/

这篇关于在C Sharp类中修改HTML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆