developer.com
Search EarthWeb
CodeGuru | Gamelan | Jars | Wireless | Discussions
Navigate developer.com
Architecture & Design  
Database  
Java
Languages & Tools
Microsoft & .NET
Open Source  
Project Management  
Security  
Techniques  
Voice  
Web Services  
Wireless/Mobile
XML  
New
 
Technology Jobs  

   Developer.com Webcasts:
  The Impact of Coding Standards and Code Reviews

  Project Management for the Developer

  Defining Your Own Software Development Methodology

  more Webcasts...




Nominate the Best Products or Technologies for Developer.com Product of the Year!




Developer Jobs

Be a Commerce Partner














 


Developer News -
Latest Linux Hits Networking Flaws    October 10, 2008
Metasploit 3.2 Offers More 'Evil Deeds'    October 8, 2008
'Thank You Apple. Seriously.'    October 8, 2008
The Buzz: BlackBerry App Store Seen Next    October 7, 2008
Free Tech Newsletter -

Parsing HTML in Microsoft C#
By Jeff Heaton

Go to page: 1  2  Next  

Most data on the Web is stored in the Hypertext Markup Language (HTML) format. There are many times that you might want to parse HTML in your C# application. However, the .NET framework does not provide an easy way to parse HTML. Evidence of this is the numerous questions posted by C# programmers looking for an easy way to parse HTML.

The Microsoft .NET framework includes extensive support for Extensible Markup Language (XML). However, although XML and HTML look very similar, they are not very compatible. Consider the following major differences between XML and HTML:

  • XML requires end tags.
  • All XML attribute values must be fully quoted with either single or double quotes.
  • XML tags must be properly nested.
  • XML tag names are case sensitive.
  • XML does not allow duplicate attributes.
  • Empty attributes are not allowed in XML.

Let's look at one of these examples in code to illustrate the difference. In XML, every beginning tag must have an ending tag. The following HTML would cause problems for a XML parser.

<p>This is line 1<br>
This is line 2</p>

This is just one of many differences. Of course, you can require HTML to be written in such a way that it is compatible with XML. The preceding HTML could be rewritten as in the following example.

<p>This is line 1<br/>
This is line 2</p>

Both an XML parser and any modern browser could understand this. Unfortunately, this is not a viable solution because you do not control the sources of HTML. You will want your program to be able to process HTML from any source.

The Solution

Because of this, I found it necessary to write my own HTML parser. In this article, I will show you how my HTML parser was constructed, and how you can use this parser with your own applications. I will begin by showing you the main components that make up the HTML parser. I will conclude this article by showing a simple example that uses the HTML parser.

The HTML parser consists of the following four classes:

  • Attribute—The attribute class is used to hold an individual attribute inside an HTML tag.
  • AttributeList—The attribute list holds an individual HTML tag and all of its attributes.
  • Parse—Holds general text parsing routines.
  • ParseHTML—The main class that you will interface with; the ParseHTML class is fed the HTML that you would like to parse.

I will now show you how each of these classes functions, and how you will use them. I will begin with the Attribute class.

The Attribute Class

The Attribute class is used to store individual HTML attributes. The source code for the Attribute class can be seen in Listing 1. The following HTML tag demonstrates attributes:

<img src="picture.gif" alt="Some Picture">

The above HTML tag has two attributes named "src" and "alt". The values of these two attributes are "picture.gif" and "Some Picture", respectively.

The Attribute class consists of three properties named "name", "value", and "delim". The "name" property stores the name of the attribute. The "value" property stores the value held by the property. And finally, the "delim" property holds the character that was used to delimit the value, if any. This property will either hold a quote ("), an apostrophe ('), or nothing at all, depending on what was used to delineate the value.

The AttributeList Class

An HTML tag often consists of several attributes. The "AttributeList" class is used to hold a list of these attributes. The "AttributeList" class is shown in Listing 2. The "AttributeList" class consists of a name and a collection of attributes. The "AttributeList" name, stored in a property called "name", holds the name of the tag. When tags are returned to you from the parser, they will be in the form of "AttributeList" objects.

The AttributeList class makes use of the C# indices. You can access individual attributes both by numeric and string indicies. For example, if an attribute "src" were stored in the "AttributeList" object "theTag", you could access the "src" attribute in the following ways:

theTag[0]    // assuming "src" were the first attribute
theTag["src"]

Both of these methods could be used to access the attributes of the tag.

Go to page: 1  2  Next  


Tools:
Add www.developer.com to your favorites
Add www.developer.com to your browser search box
IE 7 | Firefox 2.0 | Firefox 1.5.x
Receive news via our XML/RSS feed


Visual C# Archives








JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Solutions
Whitepapers and eBooks
IBM Whitepaper: Innovative Collaboration to Advance Your Business
Internet.com eBook: Real Life Rails
Avaya Article: Call Control XML - Powerful, Standards-Based Call Control
Tripwire Whitepaper: Seven Practical Steps to Mitigate Virtualization Security Risks
Internet.com eBook: The Pros and Cons of Outsourcing
Go Parallel Article: Scalable Parallelism with Intel(R) Threading Building Blocks
Internet.com eBook: Best Practices for Developing a Web Site
IBM CXO Whitepaper: The 2008 Global CEO Study "The Enterprise of the Future"
Avaya Article: Call Control XML in Action - A CCXML Auto Attendant
Go Parallel Article: James Reinders on the Intel Parallel Studio Beta Program
IBM CXO Whitepaper: Unlocking the DNA of the Adaptable Workforce--The Global Human Capital Study 2008
Adobe Acrobat Connect Pro: Web Conferencing and eLearning Whitepapers
Go Parallel Article: Getting Started with TBB on Windows
HP eBook: Storage Networking , Part 1
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
Go Parallel Video: Intel(R) Threading Building Blocks: A New Method for Threading in C++
HP Video: Is Your Data Center Ready for a Real World Disaster?
Microsoft Partner Portal Video: Microsoft Gold Certified Partners Build Successful Practices
HP On Demand Webcast: Virtualization in Action
Go Parallel Video: Performance and Threading Tools for Game Developers
Rackspace Hosting Center: Customer Videos
Intel vPro Developer Virtual Bootcamp
HP Disaster-Proof Solutions eSeminar
HP On Demand Webcast: Discover the Benefits of Virtualization
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
Microsoft Download: Silverlight 2 Software Development Kit Beta 2
30-Day Trial: SPAMfighter Exchange Module
Red Gate Download: SQL Toolbelt
Iron Speed Designer Application Generator
Microsoft Download: Silverlight 2 Beta 2 Runtime
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
IBM IT Innovation Article: Green Servers Provide a Competitive Advantage
Microsoft Article: Expression Web 2 for PHP Developers--Simplify Your PHP Applications
Featured Algorithm: Intel Threading Building Blocks - parallel_reduce
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES