我试图在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);
}
}
最佳答案
您应该能够使用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);
}
}
关于c# - 从缓冲区/流读取的html解析器,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7648254/