Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/273.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Html Agility Pack能否基于name属性以元素为目标?_C#_Html_Html Agility Pack - Fatal编程技术网

C# Html Agility Pack能否基于name属性以元素为目标?

C# Html Agility Pack能否基于name属性以元素为目标?,c#,html,html-agility-pack,C#,Html,Html Agility Pack,使用,是否可以通过查找特定元素的name属性而不是其在节点列表中的索引位置来确定其目标值?例如,如果表单元素的顺序发生变化,那么name=description的元素将如何成为目标,并检查它是否存在于表单中 HTML示例: <form> <input type="hidden" name="title" value="example"> <input type="hidden" name="body" value="example">

使用,是否可以通过查找特定元素的name属性而不是其在节点列表中的索引位置来确定其目标值?例如,如果表单元素的顺序发生变化,那么name=description的元素将如何成为目标,并检查它是否存在于表单中

HTML示例:

<form>
    <input type="hidden" name="title" value="example">
    <input type="hidden" name="body" value="example">
    <input type="hidden" name="description" value="example">
    <input type="hidden" name="category" value="example">
    <input type="hidden" name="date" value="example">
</form

通过在Html Agility Pack中使用XPath,您可以搜索Html文档中的几乎任何内容

您正在使用的XPath会查找整个表单或输入列表,但不会查找特定内容

如果您需要一个具有属性name的特定元素,该属性保存description的值,那么可以使用下面的xpath进行查找

var doc = new HtmlDocument();
doc.Load(file);

var inputNode = doc.DocumentNode.SelectSingleNode("//input[@name='description']");

if (inputNode == null)
{
   Console.WriteLine("Empty Gift Box"); // nothing found.
}
else
{
   Console.WriteLine(inputNode.Attributes["value"].Value.ToString());
}
// Prints 'example'

// Or you can use a single line lookup and assignment.
// Note.. ?. means if the result was null / not found, set left side = null.
Obj.Title = doc.DocumentNode.SelectSingleNode("//input[@name='title']")?.Attributes["value"]?.Value.ToString();
它是如何工作的

//搜索整个文档 /查找紧跟在标记后面的标记//表单/输入。如果表单和输入之间有另一个标记,则会产生0个结果。 .//在调用SelectNodes的特定标记下搜索任何标记。如果在外部有输入标记,它们将不会被捕获到孙子下

var parentNode = doc.DocumentNode.SelectNodes("//form");
var grandChild = parentNode.SelectNodes(".//input"); // must be part of form.
如果要搜索输入的特定索引、属性或值,可以使用关键字搜索

// Get any first node that has both name and value
var specificNode = doc.DocumentNode.SelectSingleNode("//input[@name and @value]");  

// Get first node that has specific name and value
var specificNode2 = doc.DocumentNode.SelectSingleNode("//input[@name='description' and @value='example']");

// Get 3rd input element
var thirdInput = doc.DocumentNode.SelectSingleNode("//input[3]");
在文档中还有许多其他的搜索方法,但这应该可以让您理清头绪。请确保您阅读了StackOverflow上的内容,并在下面找到了许多答案

别忘了把回答你帖子的帖子标为答案

// Get any first node that has both name and value
var specificNode = doc.DocumentNode.SelectSingleNode("//input[@name and @value]");  

// Get first node that has specific name and value
var specificNode2 = doc.DocumentNode.SelectSingleNode("//input[@name='description' and @value='example']");

// Get 3rd input element
var thirdInput = doc.DocumentNode.SelectSingleNode("//input[3]");