Load HtmlDocument from MemoryStream

c# html-agility-pack

Question

I have a stream that is being used by multiple HtmlDocument types, but Stream.CanSeek is false so once I read the stream the first time I'm unable to pass that same stream to another HtmlDocument object to load it.

I was thinking I could copy the stream to a MemoryStream and then pass the memory stream to each HtmlDcoument but when I call document.Load(memoryStream) the document.DocumentNode is null.

What gives?

private HtmlNode getNode(MemoryStream stream) {
    var document = new HtmlDocument();
    document.Load(stream);
    return document.DocumentNode.SelectSingleNode("html/head");
}

Accepted Answer

Why loading same data several times? Load your html once into document and then use same document instance for parsing all required data.

Make your methods accept document:

private HtmlNode getNode(HtmlDocument document) 
{
    return document.DocumentNode.SelectSingleNode("html/head");
}

And then pass single document instance everywhere

var document = new HtmlDocument();
document.Load(stream);
var node1 = getNode(document);
var node2 = getOtherNode(document);

Or even consider creating class where document will be class field. Thus you will not need to pass it to each method.



Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow