Hi All,
I would like to get the following output from htm ot html files:
I would like to get the following output from htm ot html files:
1. MY WEBSITE (Means to get text between TITLE tags)
2. left (Means to align values of "p" tag)
3. boat.gif (Means to src values of "img" tag)
<7HEAD>
My First Heading
My first paragraph.

Thanks Allot in Advance
Darma
VulpesPosted May 6, 2013, 8:33 AM
string regExp3 = @"
darma tejaPosted May 6, 2013, 8:07 AM
If i have more than one image, how can get all images with their path.
Thanks,
Darma
Raj BandiPosted May 6, 2013, 7:27 AM
Im not sure why it wasn't working for you. HtmlAgilityPack is well tested and is fast.
Here is the code
static void Main(string[] args)
{
var doc = new HtmlDocument();
doc.Load("e:\\temp\\dharma.html");
string align = string.Empty,src = string.Empty,title = string.Empty;
var titleNode = doc.DocumentNode.SelectSingleNode("//title");
if(titleNode != null)
{
title = titleNode.InnerText;
}
var p = doc.DocumentNode.SelectSingleNode("//p[@align]");
if(p != null)
{
align = p.Attributes["align"].Value;
}
var img = doc.DocumentNode.SelectSingleNode("//img[@src]");
if(img != null)
{
src = img.Attributes["src"].Value;
}
Console.WriteLine(title);
Console.WriteLine(align);
Console.WriteLine(src);
}
darma tejaPosted May 6, 2013, 7:17 AM
Thanks for the replies.
@Vulpes: Perfectly, It is working.
@ Raj: First I tried with "HtmlAgilityPack", Unfortunately it was not working.
Thanks,
Darma
Raj BandiPosted May 6, 2013, 7:07 AM
Sorry my bad incorrectly interpreted, I thought it was on the client side in browser, so you want read this in C#,
Use HtmlAgilityPack http://htmlagilitypack.codeplex.com/, add references to your project.
var doc = new HtmlDocment();
doc.load("yourfile.htm");
Use xpath queries something like this
var p = doc.DocumentElement.SelectNodes("//p[@align]");
VulpesPosted May 6, 2013, 7:04 AM
As I don't know what type of application you're writing, I've used a console application for illustration:
";
The output, as expected, is:
darma tejaPosted May 6, 2013, 6:59 AM
Thanks for the reply.
Which class I should use to load my html file by giving file path.
Thanks
Darma
Raj BandiPosted May 6, 2013, 6:40 AM
var title = document.title;
//left value, Note: without an id, get p first element, with id use document.getElementById
var align = document.getElementsByTagName("p")[0].getAttribute("align");
//boat.gif
var src = document.getElementsByTagName("img")[0].getAttribute("src");
Call above scripts when dom is ready i,e. either call a function on body load or include above script between and