I'm using this code to change the href attribute of a HTML stream.
first I download a full html page using this code:(URL is webpage address)
HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(URL);
HttpWebResponse myHttpWebResponse =
(HttpWebResponse)myHttpWebRequest.GetResponse();
Stream s = myHttpWebResponse.GetResponseStream();
then I process this:
HtmlDocument doc = new HtmlDocument();
doc.Load(s);
foreach (HtmlNode link in doc.DocumentNode.SelectNodes("/a"))
{
string att = link.Attributes["href"].Value;
link.Attributes["href"].Value = "http://ahmadalli.somee.com/default.aspx?url=" + att;
}
doc.Save(s);
s
is html stream.
but I've got an exception that says doc.DocumentNode
is null!
i tried many sites but doc.DocumentNode
is null to
s
look like? – Mike Park Feb 4 '12 at 7:23HTML
element? ABODY
element? Is it a valid document? – Oded Feb 4 '12 at 8:56doc.DocumentNode.SelectNodes
could returnnull
if no nodes found. It seems your xpath expression doesn't found any node and return null. Could you paste a full code which throws aNullReferenceException
exactly ondoc.DocumentNode
? – Alex Mar 3 '12 at 16:22