【问题标题】:Read a HTML file into a string variable in memory将 HTML 文件读入内存中的字符串变量
【发布时间】:2012-08-24 10:01:01
【问题描述】:

如果我在磁盘上有一个 HTML 文件,如何在运行时将其全部读入 String 变量?然后我需要对该字符串变量进行一些处理。

一些像这样的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>

【问题讨论】:

    标签: c# html file-io html-parsing


    【解决方案1】:
    var htmlText = System.IO.File.ReadAllText(@"C:/filename.html");
    

    如果文件在应用程序根目录下,用户在下面

    var htmlText = System.IO.File.ReadAllText(HttpContext.Current.Server.MapPath(@"~/filename.html"));
    

    【讨论】:

      【解决方案2】:

      你可以用简单的方法来做:

      string pathToHTMLFile = @"C:\temp\someFile.html";
      string htmlString = File.ReadAllText(pathToHTMLFile);
      

      或者您可以使用 FileStream/StreamReader 将其流式传输:

      using (FileStream fs = File.Open(pathToHTMLFile, FileMode.Open, FileAccess.ReadWrite))
      {
          using (StreamReader sr = new StreamReader(fs))
          {
              htmlString = sr.ReadToEnd();
          }
      }
      

      后一种方法允许您打开文件,同时仍允许其他人对该文件执行读/写操作。我无法想象 HTML 文件会很大,但它具有流式传输文件的额外好处,而不是像第一种方法那样将其捕获为一个大块。

      【讨论】:

      • 后一种方法允许您在打开文件的同时仍允许其他人对文件执行读/写操作。您是否暗示第一种方法禁止其他人对文件执行读取操作?因为我不这么认为。
      • 等待您的回复@vapcguy
      • @om-ha 根本没有暗示这一点——老实说,我从来没有在做File.ReadAllText 的同时测试读写操作。但是,我已经使用后一个代码块的多种组合对其进行了测试。我的意思只是描述最后一段代码中的内容,因为FileModeFileAccess 有多种选项可以使用,但实际上并不像您认为的那样有效。
      【解决方案3】:

      使用File.ReadAllText 传递文件位置作为参数。

      但是,如果您的真正目标是解析 html,那么我建议您使用 Html Agility Pack

      【讨论】:

      • 在读入文件之前不要忘记检查文件是否存在。 :)
      • HAP 为我做了!
      【解决方案4】:

      您要进行什么样的处理?你可以在XmlDocument doc = new XmlDocument(); 后面跟着doc.Load(filename)。然后可以在内存中解析 XML 文档。

      阅读此处了解有关 XmlDocument 的更多信息:

      【讨论】:

        【解决方案5】:

        这已经基本介绍过了,但是当我在之前的代码示例中遇到问题时添加了一个。

        Dim strHTML as String = System.IO.File.ReadAllText(HttpContext.Current.Server.MapPath("~/folder/filename.html"))
        

        【讨论】:

          【解决方案6】:

          使用File.ReadAllText(path_to_file)阅读

          【讨论】:

            【解决方案7】:
            string html = File.ReadAllText(path);
            

            【讨论】:

              【解决方案8】:

              使用System.IO.File.ReadAllText(fileName)

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2012-11-10
                • 1970-01-01
                • 2013-03-28
                • 1970-01-01
                • 2012-05-24
                相关资源
                最近更新 更多