【问题标题】:Parse Html file using html agility pack c#使用 html 敏捷包 c# 解析 Html 文件
【发布时间】:2014-02-14 21:58:08
【问题描述】:

我想在 html 代码中查找所有类型的值。我使用了 html 敏捷包,这里是我的代码:

doc.Load(resp.GetResponseStream());
foreach (HtmlNode input in doc.DocumentNode.SelectNodes("//input"))
{
     HtmlAttribute value = input.Attributes["value"];
     Console.WriteLine(value);
}

代码的输出只是 htmlagilitypack.htmlattribute。你能告诉我它是什么吗?

【问题讨论】:

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


    【解决方案1】:

    表示 HTML 标签属性。如果需要它的值,则必须使用其Value 属性。

    例如:

    doc.Load(resp.GetResponseStream());
    foreach (HtmlNode input in doc.DocumentNode.SelectNodes("//input"))
    {
         HtmlAttribute attr = input.Attributes["value"];
         if (attr != null)
            Console.WriteLine(attr.Value);
    } 
    

    对于 HTML:

    <input type=text name="myInput" value="Come get some!" />
    

    输出将是:Come get some!

    编辑:添加空检查

    【讨论】:

    • 我收到此错误 Object reference not set to an instance of an object.doc 它不为空,因为在我使用此代码之前它返回两次 htmlagilitypack.htmlattribute 并且我有两行匹配这种情况.
    • 输入没有这个属性时会发生。检查 attr 是否不为空。请参阅有关此的问题:stackoverflow.com/questions/4090200/…
    猜你喜欢
    • 1970-01-01
    • 2013-07-20
    • 2012-08-23
    • 2010-10-13
    • 2010-10-27
    • 1970-01-01
    • 1970-01-01
    • 2011-04-17
    • 2017-06-01
    相关资源
    最近更新 更多