C# 将 HTML 文件读入内存中的字符串变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12183932/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Read a HTML file into a string variable in memory
提问by Bohn
If I have a HTML file on disk, How can I read it all at once in to a String variable at run time? Then I need to do some processing on that string variable.
如果我在磁盘上有一个 HTML 文件,如何在运行时将其全部读入 String 变量?然后我需要对该字符串变量进行一些处理。
Some html file like this:
一些像这样的html文件:
<html>
<table cellspacing="0" cellpadding="0" rules="all" border="1" style="border-width:1px;border-style:solid;width:274px;border-collapse:collapse;">
<COLGROUP><col width=35px><col width=60px><col width=60px><col width=60px><col width=59px></COLGROUP>
<tr style="height:20px;">
<th style="background-color:#A9C4E9;"></th><th align="center" valign="middle" style="color:buttontext;background-color:#D3DCE9;">A</th><th align="center" valign="middle" style="color:buttontext;background-color:#D3DCE9;">B</th><th align="center" valign="middle" style="color:buttontext;background-color:#D3DCE9;">C</th><th align="center" valign="middle" style="color:buttontext;background-color:#D3DCE9;">D</th>
</tr><tr style="height:20px;">
<th align="center" valign="middle" style="color:buttontext;background-color:#E4ECF7;">1</th><td align="left" valign="top" style="color:windowtext;background-color:window;">Hi</td><td align="left" valign="top" style="color:windowtext;background-color:window;">Cell Two</td><td align="left" valign="top" style="color:windowtext;background-color:window;">Actually a longer text</td><td align="left" valign="top" style="color:windowtext;background-color:window;">Final Word</td>
</tr>
</table>
</html>
采纳答案by empi
Use File.ReadAllTextpassing file location as an argument.
使用File.ReadAllText传递文件位置作为参数。
However, if your real goal is to parse html then I would recommend using Html Agility Pack.
但是,如果您的真正目标是解析 html,那么我建议使用Html Agility Pack。
回答by L.B
Use System.IO.File.ReadAllText(fileName)
用 System.IO.File.ReadAllText(fileName)
回答by Forte L.
string html = File.ReadAllText(path);
回答by Ted Spence
What kind of processing are you trying to do? You can do XmlDocument doc = new XmlDocument();followed by doc.Load(filename). Then the XML document can be parsed in memory.
你想要做什么样的处理?你可以做XmlDocument doc = new XmlDocument();跟着doc.Load(filename). 然后可以在内存中解析 XML 文档。
Read here for more information on XmlDocument:
阅读此处了解有关 XmlDocument 的更多信息:
回答by Srijan
Use File.ReadAllText(path_to_file)to read
使用File.ReadAllText(path_to_file)阅读
回答by s15199d
This is mostly covered already, but one addition as I ran into an issue with the previous code samples.
这大部分已经涵盖了,但是当我遇到了以前的代码示例的问题时,还有一个补充。
Dim strHTML as String = System.IO.File.ReadAllText(HttpContext.Current.Server.MapPath("~/folder/filename.html"))
回答by vapcguy
You can do it the simple way:
你可以用简单的方法做到这一点:
string pathToHTMLFile = @"C:\temp\someFile.html";
string htmlString = File.ReadAllText(pathToHTMLFile);
Or you could stream it in with FileStream/StreamReader:
或者您可以使用 FileStream/StreamReader 将其流式传输:
using (FileStream fs = File.Open(pathToHTMLFile, FileMode.Open, FileAccess.ReadWrite))
{
using (StreamReader sr = new StreamReader(fs))
{
htmlString = sr.ReadToEnd();
}
}
This latter method allows you to open the file while still permitting others to perform Read/Write operations on the file. I can't imagine an HTML file being very big, but it has the added benefit of streaming the file instead of capturing it as one large chunk like the first method.
后一种方法允许您打开文件,同时仍允许其他人对文件执行读/写操作。我无法想象一个 HTML 文件非常大,但它具有流式传输文件的额外好处,而不是像第一种方法那样将其捕获为一个大块。

![C# System.String[] 返回而不是数组](/res/img/loading.gif)