c# - How to use Html Agility Pack for HTML validations -
i using html agility pack validating html. below using,
public class markuperrors { public string errorcode { get; set; } public string errorreason { get; set; } } public static list<markuperrors> ismarkupvalid(string html) { var document = new htmlagilitypack.htmldocument(); document.optionfixnestedtags = true; document.loadhtml(html); var parsererrors = new list<markuperrors>(); foreach(var error in document.parseerrors) { parsererrors.add(new markuperrors { errorcode = error.code.tostring(), errorreason = error.reason }); } return parsererrors; } so input 1 shown below :
<h1>test</h1> hello world</h2> <h3>missing close h3 tag so current function return list of following errors
- start tag <h2> not found - end tag </h3> not found which fine...
my problem want entire html valid, proper <head> , <body> tags, because html later available preview, download .html files.
so wondering if check using html agility pack ?
any ideas or other options appreciated. thanks
you can check there head element or body element under html element example:
bool hashead = doc.documentnode.selectsinglenode("html/head") != null; bool hasbody = doc.documentnode.selectsinglenode("html/body") != null; these fail if there no html element, or if there no body element under html element.
note don't use kind of xpath expression "//head" because give result if head not directly under html element.
Comments
Post a Comment