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");
}
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.