다음은 컨텍스트입니다. HTMLAgilityPack을 사용하여 P 노드를 선택합니다.
var paragraphe = html.DocumentNode.SelectNodes(".//p[not(descendant::p)]");
그런 다음 for 루프를 사용하여 DOM 요소의 부모가 DIV이고 다음과 같은 특정 속성을 포함하는 경우 매번 테스트하고 싶습니다. div[@edth_correction='N']
하지만 부모 노드를 얻는 방법을 모르겠다. 이미해야 할 일을위한 모든 코드를 작성했다.
paragraphe[i].ParentNode.Attributes.Equals()
와 같은 것을 할 수 있다는 것을 알고 있지만,이 Equals에 무엇을 써야하는지, 그리고 내 경우에 사용해야하는 것이 무엇인지는 알지 못합니다.
이 방법을 시도해보십시오.
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
}
}
또 다른 방법은 XPath로 부모 노드와 속성을 확인하는 것입니다.
var paras = html.DocumentNode.SelectNodes(
"//p[not(descendant::p) and name(..)='div' and ../@edth_correction='N']");
foreach (var p in paras)
{
// do work
}
조상 노드를 테스트하려면 다음을 시도하십시오.
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
}
}
}
또는 XPath 사용
var paras = html.DocumentNode.SelectNodes(
"//p[not(descendant::p) and ancestor::div/@edth_correction='N']");
foreach (var p in paras)
{
// do work
}
나는 두 번째 접근법에 대해 확신하지 못한다. 데이터 원본을 모르기 때문에
또한 XPath를 시도 할 수 있습니다.
"//p[not(descendant::p) and ancestor::*[name(.)='div' and ./@edth_correction='N']]"