Solved

Parse HTML tags from text file.

Posted on 2014-03-27
5
292 Views
Last Modified: 2014-03-28
Hi Experts,

If I wanted to retrieve the <TR> tags from a web page I would do it like this:

Dim tagCollection As HtmlElementCollection
tagCollection = WebBrowser1.Document.Body.Document.GetElementsByTagName("tr")

How do I retrieve the tags from the html file text something like this?

tagCollection = hmtlFileText.GetElementsByTagName("tr")
0
Comment
Question by:DColin
  • 3
  • 2
5 Comments
 
LVL 23

Accepted Solution

by:
Jens Fiederer earned 500 total points
ID: 39959839
Arbitrary files are not structured as HTML.  It is not hard to find all instances of "<TR>" doing simple text searches, or maybe "<TR" if you don't want to miss TR elements with attributes.  But to really structure it you need to have it parsed.

If you are fortunate enough to be using XHTML, you can use .NET XML parsing functions.

Otherwise you'll probably need a 3rd party library like HTML Agility

See http://htmlagilitypack.codeplex.com/
0
 

Author Comment

by:DColin
ID: 39959909
jensfiederer,

The text file will be an HTML file.
0
 
LVL 23

Expert Comment

by:Jens Fiederer
ID: 39959990
Yes, but from the point of view of .NET, it is just a sequence of characters (that just HAPPEN to satisfy HTML syntax).  

ASP.Net needs to GENERATE HTML, but it doesn't usually need to read it - that's the browsers job.  That's why libraries like HTML Agility exist.
0
 

Author Comment

by:DColin
ID: 39960024
jensfiederer,

I was thinking that the WebBrowser control uses the HtmlDocument class to hold the html text. So how do I load the html text into an HtmlDocument object without having to use a WebBrowser control?
0
 
LVL 23

Expert Comment

by:Jens Fiederer
ID: 39960057
Like I said, you can use HTML agility pack.  It's free, it's available at the URI I provided, and it supports code like:

HtmlDocument doc = new HtmlDocument();
 doc.Load("file.htm");
 foreach(HtmlNode link in doc.DocumentElement.SelectNodes("//a[@href"])
 {
    HtmlAttribute att = link["href"];
    att.Value = FixLink(att);
 }
 doc.Save("file.htm");

Open in new window


Note: I'm not involved in any way with the HTML Agility project, except that I needed to parse HTML files at one point a year or two ago and that is what I ended up using.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now