Reputation: 883
I am using HTMLAGility Pack to parse HTML file as I want to access attributes of DIVS in HTML.
Following my code
HtmlAgilityPack.HtmlDocument htmlDoc = new HtmlAgilityPack.HtmlDocument();
htmlDoc.Load("C:\\sampleHtml.html");
var divs = htmlDoc.DocumentNode.SelectNodes("//div");
List<Feature> pageTitles = new List<Feature>();
foreach (var div in divs)
{
pageTitles.Add(new Feature(Convert.ToInt32(div.Id), div.Name.ToString(), false, false));
}
This is my HTML
<div id="101" isEnabled="0">My Binders<br />
<img align="" width="170" vspace="0" hspace="0" height="113" border="0" alt="" src="http://www.obout.com/editor_new/images/Nature/field_from_woods.jpg" title="" /><br />
<div id="111" isEnabled="0">Share Binders<br />
<img align="" width="170" vspace="0" hspace="0" height="114" border="0" alt="" src="http://www.obout.com/editor_new/images/Nature/meadow_cows.jpg" title="" /><br />
</div>
<div id="123" isEnabled="0">Add Binders<br />
<img align="" width="48" vspace="0" hspace="0" height="48" border="0" alt="" src="http://www.obout.com/editor_new/images/flags/shadow/flag_american_samoa.png" title="" /><br />
</div></div>
I have a "IsEnabled" property for each div. But, I am not able to access the value of this property using HTMLAgile pack. How can this be achieved.
Thanks
Upvotes: 0
Views: 125
Reputation: 138906
Something like this in a sample Console Application:
HtmlDocument doc = new HtmlDocument();
doc.Load("C:\\sampleHtml.html");
foreach (HtmlNode node in doc.DocumentNode.SelectNodes("//div"))
{
Console.WriteLine(node.GetAttributeValue("isEnabled", null));
}
will dump all the value of the isEnabled
attribute.
Upvotes: 1
Reputation: 9566
Try this:
class CustomAttributesParser
{
private static HtmlDocument BuildHtmlDocument()
{
string html = @"<div id=""101"" isEnabled=""0"">My Binders<br />
<img align="""" width=""170"" vspace=""0"" hspace=""0"" height=""113"" border=""0"" alt="""" src=""http://www.obout.com/editor_new/images/Nature/field_from_woods.jpg"" title="""" /><br />
<div id=""111"" isEnabled=""0"">Share Binders<br />
<img align="""" width=""170"" vspace=""0"" hspace=""0"" height=""114"" border=""0"" alt="""" src=""http://www.obout.com/editor_new/images/Nature/meadow_cows.jpg"" title="""" /><br />
</div>
<div id=""123"" isEnabled=""0"">Add Binders<br />
<img align="""" width=""48"" vspace=""0"" hspace=""0"" height=""48"" border=""0"" alt="""" src=""http://www.obout.com/editor_new/images/flags/shadow/flag_american_samoa.png"" title="""" /><br />
</div></div>";
var doc = new HtmlDocument();
doc.LoadHtml(html);
return doc;
}
internal IEnumerable<string> Parse()
{
HtmlDocument doc = BuildHtmlDocument();
var divs = doc.DocumentNode.SelectNodes("//div");
if (divs != null)
{
return divs.Select(e => e.GetAttributeValue("isEnabled", String.Empty));
}
return Enumerable.Empty<string>();
}
}
class Program
{
static void Main(string[] args)
{
var parser = new CustomAttributesParser();
parser.Parse()
.ToList()
.ForEach(Console.WriteLine);
}
}
Upvotes: 0
Reputation:
Actually it's not possible to add custom attributes to elements. They won't be parsed.
In HTML5 you can use the data-attributes
:
<div data-Enabled="0">..</div>
Upvotes: 1