HTMLAgilitypack testing parent node attribute value

c# html-agility-pack

Question

What follows is the setting: Using HTMLAgilityPack, I choose the P node as follows:

var paragraphe = html.DocumentNode.SelectNodes(".//p[not(descendant::p)]");

Then, using a for loop, I'd want to check, for each iteration, if this DOM element's parent is a DIV and whether it includes a certain attribute, such as:div[@edth_correction='N']

I have already developed all the necessary code for the task I need to accomplish, but I have no clue how to get the parent node.

I am certain that I can carry out the following:paragraphe[i].ParentNode.Attributes.Equals() However, I have no clue what to put in this Equals or whether I should use it in my situation.

1
1
6/24/2016 11:10:31 AM

Accepted Answer

Attempt this

var paragraphe = html.DocumentNode.SelectNodes(".//p[not(descendant::p)]");

for (int i = 0; i < paragraphe.Count; i++)
{
    var parent = paragraphe[i].ParentNode;

    if (parent.Name == "div" &&
        parent.ChildAttributes("edth_correction").Any(a => a.Value == "N"))
    {
        // do work
    }
}

Another option is to use XPath to verify the parent node and attribute.

var paras = html.DocumentNode.SelectNodes(
    "//p[not(descendant::p) and name(..)='div' and ../@edth_correction='N']");

foreach (var p in paras)
{
    // do work
}

Try this to verify node ancestors.

var paragraphe = html.DocumentNode.SelectNodes(".//p[not(descendant::p)]");

for (int i = 0; i < paragraphe.Count; i++)
{
    foreach (var ancestor in paragraphe[i].Ancestors("div"))
    {
        if (ancestor.ChildAttributes("edth_correction").Any(a => a.Value == "N"))
        {
            // do work
        }
    }
}

also using XPath

var paras = html.DocumentNode.SelectNodes(
    "//p[not(descendant::p) and ancestor::div/@edth_correction='N']");

foreach (var p in paras)
{
    // do work
}

With regard to the second strategy, I'm unsure. Since I am unfamiliar with your data source.

You may also try XPath.

"//p[not(descendant::p) and ancestor::*[name(.)='div' and ./@edth_correction='N']]"
2
6/27/2016 1:19:00 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow