
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
822
|
Chapter 14: Web
This code will have the following output:
Form element found: form1
Input Element found: Text1
Input Element found: Checkbox1
Input Element found: Radio1
Input Element found: TextBox1
Discussion
There are many ways that HTML can be parsed: regular expressions, straight text
parsing, or even third-party product offerings. The
MSHTML parser is free and preva-
lent but it is COM-based. Being COM-based in a .NET world carries a price tag of
having to have all calls go through the COM interop layer and have items marshaled
back and forth.
MSHTML can be made to perform decently in many situations, but this
is not a solution for high-end HTML parsing due to the overhead that will be
incurred each time the COM interop layer is traversed. This should be considered a
client-side operation only. The overhead would quickly degrade performance in a
server-side scenario like an
HttpHandler or in a high-traffic web page.
if (htmlElem is HTMLAnchorElementClass)
{
HTMLAnchorElementClass anchor = htmlElem as HTMLAnchorElementClass;
if (anchor != null)
Console.WriteLine("Anchor element found: " + anchor.href);
}
else if (htmlElem is HTMLFormElementClass)
{
HTMLFormElementClass form = htmlElem as HTMLFormElementClass;
if (form != null)
Console.WriteLine("Form element found: " + form.id); ...