0

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

4

3 回答 3

1

实际上,不可能向元素添加自定义属性。它们不会被解析。

在 HTML5 中,您可以使用data-attributes

<div data-Enabled="0">..</div>
于 2013-05-23T12:13:27.763 回答
1

示例控制台应用程序中的类似内容:

HtmlDocument doc = new HtmlDocument();
doc.Load("C:\\sampleHtml.html");
foreach (HtmlNode node in doc.DocumentNode.SelectNodes("//div"))
{
    Console.WriteLine(node.GetAttributeValue("isEnabled", null));
}

将转储该isEnabled属性的所有值。

于 2013-05-23T12:32:55.157 回答
0

尝试这个:

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);
    }
}
于 2013-05-23T12:27:00.127 回答