我有一个html并使用带有c#的htmlagilitypack。我需要获取特定的子节点值。我需要得到孩子的文字" strong / em"在HTML中。我怎么才能得到它?
<div id="top3">
<li> February 1, 2016:
<a target="_blank" href="test1.html">
<strong>
<em>test1</em></strong>
</a>
<br><strong>desc1</strong></li>
<li> February 2, 2016:
<a target="_blank" href="test2.html">
<strong>
<em>test2</em></strong>
</a>
<br><strong>desc2</strong></li>
</div>
foreach (HtmlNode node in doc.DocumentNode.SelectNodes("//div[@id='top3']//li"))
{
sb.Append("<p'>" + node.SelectSingleNode("a[1]/preceding-sibling::text()[1]").InnerText + "</p>");
linkNode = node.SelectSingleNode("a[1]");
sb.Append("<p class='event'><a href='" + linkNode.Attributes["href"].Value + "'>" +
//**********Need to have text from /strong/em/ here *********
+ "</a></p>");
}
答案 0 :(得分:0)
通过使用相对于所选<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>
<body>
<div>
<input type="text" id="fname" placeholder ="Full Name" name="fullname">
<input type="text" id="job" placeholder ="Job" name="job">
<input type="text" id="country" placeholder ="Country" name="country">
<input type="text" id="city" placeholder ="City" name="city">
<input type="submit" value="Submit">
<p id="p1"></p>
</div>
<script>
document.querySelector('input[type="submit"]').onclick = function() {
function Person(fullname, job, country, city) {
this.fullname = fullname;
this.job = job;
this.country = country;
this.city = city;
}
var fullname = document.getElementById("fname").value;
var job = document.getElementById("job").value;
var country = document.getElementById("country").value;
var city = document.getElementById("city").value;
var me = new Person(fullname, job, country, city);
document.getElementById("p1").innerHTML = me.fullname + ". Congratulations an object has been made with your information stored in it, the following information is what you have given us. Your job is a " + me.job + "You live in" + me.city + "," + me.country + ".";
};
</script>
</body>
</html>
元素的简单XPath,您可以获得strong/em
的值,如下所示:
<a>
此处简短演示:https://dotnetfiddle.net/r49Luk
您已经在发布的代码段中演示了使用稍微复杂的XPath表达式,所以我有点想知道为什么会出现这个问题......