2011-10-04 25 views
2

我想在使用HTTP模块的浏览器中呈现之前更改HTML页面。我试图实现敏捷性HTML解析器,但它似乎只是从文件读取。从缓冲区/流中读取的敏捷性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); 
     } 
    } 
+0

这是什么重写Write方法?它来自哪里? –

回答

1

在下载选项卡文档,您应该能够使用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); 
    } 
} 
1

实际上HtmlDocument.Load()方法被重载和包含用于加载流定义:(流)负载,负载(流,布尔值),负载(流,编码)。

你可以找到http://htmlagilitypack.codeplex.com/