Your problem is how you load data into an HtmlDocument . To load data from a file, you must use the Load(fileName) method. But you use the LoadHtml(htmlString) method, which treats "E:\\file1.htm" as the contents of the document. When HtmlAgilityPack tries to find h2 tags in the line E:\\file1.htm , it does not find anything. Here is the correct way to load the html file:
string filePath = "E:\\file1.htm"; htmlDoc.Load(filePath);
Also, @Simon Mourier correctly pointed out that you should use the SelectSingleNode method if you are trying to get a single node:
// Single HtmlNode var patient = doc.DocumentNode.SelectSingleNode("//h2[@id='hd1'"); patient.InnerHtml = "Patient SurName";
Or, if you are working with a set of nodes, then process them in a loop:
// Collection of nodes var patients = doc.DocumentNode.SelectNodes("//div[@class='patient'"); foreach (var patient in patients) patient.SetAttributeValue("style", "visibility: hidden");
source share