【发布时间】:2013-12-22 19:05:41
【问题描述】:
HTML
<html>
<head>
<title>Sample Page</title>
</head>
<body>
<form action="demo_form.asp" id="form1" method="get">
First name: <input type="text" name="fname"><br>
Last name: <input type="text" name="lname"><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
代码
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(File.ReadAllText(@"C:\sample.html"));
HtmlNode nd = doc.DocumentNode.SelectSingleNode("//form[@id='form1']");
//nd.InnerHtml is "".
//nd.InnerText is "".
问题
nd.ChildNodes //Collection(to get all nodes in form) is always null.
nd.SelectNodes("/input") //returns null.
nd.SelectNodes("./input") //returns null.
"//form[@id='form1']/input" //returns null.
我想要的是按出现的顺序一一访问 id=form1 的表单标签的子节点。我在 chrome 开发人员控制台中尝试了相同的 xpath,它完全按照我想要的方式工作。 HTMlAgility 包是否在从文件或 Web 读取 html 时遇到问题。
【问题讨论】:
-
问题是什么?
-
我想要 htmlNodecollection 中的 Forms 标签的所有子节点。
标签: c# html-agility-pack