Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/292.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# HtmlAgilityPack从子节点选择数据_C#_Html_Linq_Parsing_Html Agility Pack - Fatal编程技术网

C# HtmlAgilityPack从子节点选择数据

C# HtmlAgilityPack从子节点选择数据,c#,html,linq,parsing,html-agility-pack,C#,Html,Linq,Parsing,Html Agility Pack,我想解析以下HTML代码: <h3 class='bar'> <a href='http://anysite.com/index.php?showuser=7195' title='Profile view'>THIS_IS_USERNAME</a> &nbsp; <a href='http://anysite.com/index.php?showuser=7195&amp;f=' class='__user __id7195

我想解析以下HTML代码:

<h3 class='bar'>
  <a href='http://anysite.com/index.php?showuser=7195' title='Profile view'>THIS_IS_USERNAME</a>
  &nbsp;
  <a href='http://anysite.com/index.php?showuser=7195&amp;f=' class='__user __id7195' title='Profile view'>
      <img src='http://anysite.com/public/style_images/car/user_popup.png' alt='' />
   </a>
</h3>

这里我需要的是选择用户名(“THIS_is_username”)和到配置文件的链接(“”)

我可以使用下一个代码选择顶部h3节点:

List<HtmlNode> resultSearch = HTMLPage.DocumentNode.Descendants()
                .Where(
                         x => x.Name.Equals("h3")
                         && x.Attributes["class"] != null
                         && x.Attributes["class"].Value.Equals("bar")                         
                      )
                .ToList();
List resultSearch=HTMLPage.DocumentNode.subjects()
.在哪里(
x=>x.Name.Equals(“h3”)
&&x.Attributes[“类”]!=null
&&x.属性[“类”].值等于(“条”)
)
.ToList();

但是我如何才能不获取“h3”节点本身,而是获取“h3”中的“a”,该属性链接包含用户名和我需要的配置文件链接?

您可以直接查询链接节点,它与标题属性非常不同

在这种情况下,使用XPath可能更简单,因为它可以处理所有中间空检查,而且它也是类型安全的,因为您的Linq查询将有很多常量字符串:

var node = HTMLPage.DocumentNode.SelectSingleNode("//hr[@class='Bar']/a[@title='Profile View' and @href");
if (node != null)
{
    string link = node.Attributes["href"].Value;
    string username = node.InnerText;
}
您可以使用Linq语法编写类似的代码,它首先搜索链接标记,然后回溯以找到它的h3父级。这样,您就不必检查中间空值;):

或者您可以使用它作为“bar”的第一个子项的位置:


非常感谢。这正是我要找的!虽然最后一部分使用了新的HtmlNode(“h3”).Elements(“a”).FirstOrDefault();有点不清楚,似乎不起作用。我想这可以解决问题。
var node = HtmlPage.DocumentNode.Descendants("a")
    .Where(a =>
        a.Ascendants("h3")
            .Any(h3 =>
                h3.Attributes["class"] != null 
                && a.Attributes["class"].Value == "bar"
            )
    )
    .Where(a => 
        a.Attributes["title"] != null 
        && a.Attributes["title"].Value == "Profile View"
        && a.Attributes["href"] != null
    )
    .FirstOrDefault();

if (node != null)
{
    string link = node.Attributes["href"].value;
    string username = node.InnerText;
}
// the call to First() will throw an exception if the h3 isn't found.
// returning an empty HtmlNode will allow you to ignore that

var node = (HtmlPage.DocumentNode.Descendants("h3")
    .FirstOrDefault( h => 
            h3.Attributes["class"] != null 
            && a.Attributes["class"].Value == "bar")
    ) ?? HtmlPage.CreateElement("h3")) 
    .Elements("a").FirstOrDefault();

if (node != null)
{
    string link = node.Attributes["href"].value;
    string username = node.InnerText;
}