【问题标题】:Find the node that has no children. If it has any <span> children, just ignore it找到没有子节点的节点。如果它有任何 <span> 孩子,忽略它
【发布时间】:2012-11-19 14:39:23
【问题描述】:

我正在使用 HTMLAgilityPack。我有这样的事情:

<div class="address">
  <h3>Postadress</h3>
  <div class="box-address">Box  27 </div>
  <div class="post-address">
    16493 KISTA 
  </div>
</div>

问题是还有其他&lt;div class="address"&gt;s。 所以我必须找到一个有&lt;h3&gt; 孩子的那个,上面写着“邮政地址”。 我需要提取的是&lt;div class="post-address"&gt; 的值,即“16493 KISTA”。 &lt;div class="post-address"&gt; 还返回了其他有孩子的记录,我不希望这些记录被返回。我只是在寻找没有孩子并且只包含裸文本的&lt;div class="post-address"&gt;

到目前为止我的解决方案是:

var postAddressdiv = doc.DocumentNode.SelectNodes("//div[@class='address']");
if (postAddressdiv != null)
{
    foreach (HtmlAgilityPack.HtmlNode node in postAddressdiv)
    {
        HtmlNode postAddress;
        var h3 = node.Descendants("h3");
        if (h3 != null)
        {
            if (h3.First().LastChild.InnerHtml == "Postadress")
            {
                MessageBox.Show("right place you are.");
                postAddress = node.SelectSingleNode("//div[@class='post-address']");
                var postAddressChildren = postAddress.Descendants();
                if (postAddressChildren == null)
                    MessageBox.Show("found one!!!!");
            }
        }
    }
}

但它不起作用。我究竟做错了什么?谢谢。

【问题讨论】:

  • 为什么“不工作”?

标签: c# .net html xpath html-agility-pack


【解决方案1】:
var nodes  = doc.DocumentNode
            .SelectNodes("//div[@class='address' and h3='Postadress']/div[@class='post-address']");

【讨论】:

    猜你喜欢
    • 2018-11-11
    • 2014-07-21
    • 1970-01-01
    • 1970-01-01
    • 2022-01-25
    • 2014-09-11
    • 1970-01-01
    • 1970-01-01
    • 2013-08-30
    相关资源
    最近更新 更多