Hi,
I'd like to know one (or more) ways to parse the HTML page output. I'd like to detect some patterns on the HTML that will be send to the client and log some info if present.
views:
27answers:
2
+1
A:
It depends on what you mean by "parse" exactly, but something like the HTML Agility Pack can create an XML-like structure from an HTML document - essentially creating a proper HTML DOM data structure. You can even then convert it straight to XML, use LINQ, etc.
Rex M
2010-09-27 15:14:42
@Rex M: I'd like to accomplish this with something simpler if possible. I'm trying to detect a problem so this is just for helping the troubleshoot process, just a temporal code. I'd like to process the HTML output before sending it to the browser. Thanks!
Timmy O' Tool
2010-09-27 15:19:56
@Timmy it'd get you a pretty straightforward answer if you say what you are trying to detect
eglasius
2010-09-27 16:30:26
@Rex M: let's say I want to detect if a table with id="mycontrol" exist on the html that will be send to the client. Because adaptative rendering sometimes "mycontrol" is rendered as div and sometimes as table. I want to detect cases where divs will be rendered as table.
Timmy O' Tool
2010-09-27 17:00:50
+1
A:
Everything you need is in the
Page.Render
method, override it and do what you want to in there.
protected override void Render(HtmlTextWriter writer)
{
// do your stuff here
StringBuilder stringBuilder = new StringBuilder();
StringWriter stringWriter = new StringWriter(stringBuilder);
HtmlTextWriter htmlTextWriter = new HtmlTextWriter(stringWriter);
base.Render(htmlTextWriter); // <-- render the page into the htmlTextwriter
// the htmlTextwriter connects trough the stringWriter to the stringBuilder
string theHtml = stringBuilder.ToString(); // <---- html captured in string
//---------------------------------------------
//do stuff on theHtml here
//---------------------------------------------
writer.Write(theHtml); // <----write html with the original writer
}
Caspar Kleijne
2010-09-27 17:04:01
@Caspar: thanks. And how do I get the string output from the writer?
Timmy O' Tool
2010-09-27 17:18:03