【问题标题】:issue in Html parsing in C#C#中的Html解析问题
【发布时间】:2013-05-18 18:55:39
【问题描述】:

我正在使用 HTMLAGility Pack 解析 HTML 文件,因为我想在 HTML 中访问 DIVS 的属性。

按照我的代码

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));
}

这是我的 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>

每个 div 都有一个“IsEnabled”属性。但是,我无法使用 HTMLAgile 包访问此属性的值。这是如何实现的。

谢谢

【问题讨论】:

  • 您如何尝试访问isEnabled 属性的值?
  • var divs = htmlDoc.DocumentNode.SelectNodes("//div");给出了一个 DIV 的列表,其中我得到了所有属性,但不是用户定义的。否则,我试过了, var list = htmlDoc.DocumentNode.SelectNodes("//div@IsEnabled");

标签: c#-4.0 html-agility-pack


【解决方案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 属性的所有值。

【讨论】:

    【解决方案2】:

    试试这个:

    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);
        }
    }
    

    【讨论】:

      【解决方案3】:

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

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

      <div data-Enabled="0">..</div>
      

      【讨论】:

      • 如果我使用 HTML5 的数据属性,它们会在 C# 中解析
      • 我做了一些研究:Html Agility Pack 应该能够毫无问题地解析 HTML5,只要提供了预期的有效 HTML 的逻辑结构
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-06-12
      • 2015-06-06
      • 2013-04-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-21
      • 1970-01-01
      相关资源
      最近更新 更多