Итак, я пытаюсь разобрать html с помощью xpath. Вот как выглядит моя страница
<area id = "a_0_0" alt = "asd">
<area id = "a_0_1" alt = "asd">
<area id = "a_1_0" alt = "asd">
<area id = "a_1_1" alt = "asd">
так можно ли разобрать его с помощью xpath? или мне нужно использовать что-то еще? Я немного новичок в xpath, вот что я пробовал
doc.DocumentNode.SelectSingleNode("//area[@id='a_0_0']").Attributes["alt"].Value;//this is works
doc.DocumentNode.SelectSingleNode("//area[@id='a_0_[position() >=0 and position() <=1]']").Attributes["alt"].Value
doc.DocumentNode.SelectSingleNode("//area[@id='a_[position() >=0 and position() <=1]']_[position() >=0 and position() <=1]']").Attributes["alt"].Value
Вы можете разобрать свой xml в XDocument и извлечь выгоду из linq.
XDocument.Parse(xmlstring)
Вот простое решение.
var nodes = doc.DocumentNode.SelectNodes("//area[contains('a_0_0,a_0_1,a_1_0,a_1_1',id)]")
foreach (var node in nodes)
{
// node.Attributes["alt"].Value
}
Также вы можете использовать ниже, чтобы получить все узлы области, которые имеют атрибут alt.
var nodes = doc.DocumentNode.SelectNodes("//area[@alt]");
Чтобы получить все узлы области, начинающиеся с a_
, используйте приведенное ниже.
var nodes = doc.DocumentNode.SelectNodes("//area[starts-with(@id,'a_')][@alt]);