【问题标题】:agility html parser read from buffer/stream从缓冲区/流中读取的敏捷 html 解析器
【发布时间】:2011-10-04 23:01:54
【问题描述】:

我正在尝试在 HTML 页面使用 HTTP 模块在浏览器中呈现之前对其进行更改。我试图实现敏捷 HTML 解析器,但它似乎只从文件中读取。

如何从缓冲区/流中读取它?

public override void Write(byte[] buffer, int offset, int count)
    {
      byte[] data = new byte[count];
      Buffer.BlockCopy(buffer, offset, data, 0, count);
      string html = System.Text.Encoding.Default.GetString(buffer);

      HtmlDocument doc = new HtmlDocument();
      doc.Load(html);
      foreach(HtmlNode link in doc.DocumentElement.SelectNodes("//a[@href"])
      {
      HtmlAttribute att = link["href"];
      att.Value = FixLink(att);
      }
    }

【问题讨论】:

  • 这个覆盖写入方法是什么?它来自哪里?

标签: c# asp.net html html-agility-pack httpmodule


【解决方案1】:

其实HtmlDocument.Load()方法是重载的,包含了加载流的定义:Load(Stream), Load(Stream, Boolean), Load(Stream, Encoding)。

您可以在 http://htmlagilitypack.codeplex.com/ 的“下载”选项卡中找到文档

【讨论】:

    【解决方案2】:

    您应该可以使用MemoryStream 来读取数据:

    public override void Write(byte[] buffer, int offset, int count)
    {
      var stream = new MemoryStream(buffer, offset, count);
    
      HtmlDocument doc = new HtmlDocument();
      doc.Load(stream);
    
      foreach(HtmlNode link in doc.DocumentElement.SelectNodes("//a[@href"])
      {
        HtmlAttribute att = link["href"];
        att.Value = FixLink(att);
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-13
      • 2010-10-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多