How to copy html text selection and assign it to a string in c#

佐手、 提交于 2019-12-11 07:44:50

问题


I have an .html file containing only text(formatted somehow).I am wondering if there is a way to copy all of the text(like if would do with ctrl+A) and assign it to a string So I can then export it to .txt file ? All this must happen from the code-behind(opening the html, selecting the text and assigning it to a string).

In the Microsoft.Office.Interop I remember that there was an option for copying the active window selection or something, so I was wondering if this is possible and here.


回答1:


Use HttpAgilityPack. Someone could say it's overblown, but otherwise tomorrow you'll ask us how to convert the &code; that are in the file, and the next day you'll ask something else.




回答2:


use this for read from file

            using (StreamReader sr = new StreamReader("TestFile.html"))
        {
            String line = sr.ReadToEnd();
            Console.WriteLine(line);
        }

http://msdn.microsoft.com/en-us/library/db5x7c0d.aspx

and this for read from url

WebClient client = new WebClient();
String htmlCode = client.DownloadString("http://test.com/file.html");


来源:https://stackoverflow.com/questions/18350208/how-to-copy-html-text-selection-and-assign-it-to-a-string-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!