Posts

Showing posts with the label HTML Agility Pack

Donate

Invalid nested tag div found, expected closing tag input

Hello all, I've been experimenting on how to print html document using the said 3rd party software called iTextSharp . iTextSharp is a popular tool and has several examples on the internet regarding integration to the project and occurring issues. One of the issue I encountered is Invalid nested div tag and is expecting a closing tag input . As I trace back my html source, the tags are well-formed except that they are self closing tags such as <input>, <hr>, <img>, <br> or the like. These tags when passed to an action method as string are not properly closed and thus an issue is thrown by iTextSharp's XMLWorkerHelper's ParseXHtml() method. <img src= "~/Images/success.png" /> <input type= "hidden" name= "OrderStatusHTML" /> <input type= "submit" id= "btnSubmit" value= "Export to PDF" class= "btn btn-success" /> The solution I came up with is to fi

Parse Html Table Using HTML Agility Pack In C#

Below is a simple code to parse a table using HTML Agility Pack. Make sure to add the Html Agility Pack package from Nuget and reference that library in the namespace of your program. 1 using HtmlAgilityPack; Parse Table 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 HtmlWeb web = new HtmlWeb(); HtmlDocument doc = web.Load( "http://your_sample_url" ); // Get all tables in the document HtmlNodeCollection tables = doc.DocumentNode.SelectNodes( "//table" ); // Iterate all rows in the first table HtmlNodeCollection rows = tables[0].SelectNodes( "tr" ); for ( int i = 0; i <= rows.Count - 1; i++) { // Iterate all columns in this row HtmlNodeCollection cols = rows[i].SelectNodes( "td" ); if (cols != null ) { for ( int j = 0; j <= cols.Count - 1; j++) { // Get the value of the column and print it string value = cols[j].InnerText; Console.Write

Donate