HtmlAgilityPack-如何通过ID获取标签? [英] HtmlAgilityPack - How to get the tag by Id?

查看:153
本文介绍了HtmlAgilityPack-如何通过ID获取标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有任务要做.我需要检索特定idtaghref(id基于用户输入). 例子我有一个像这样的html

I have a task to do. I need to retrieve the a tag or href of a specific id (the id is based from the user input). Example I have a html like this

<manifest>

<item href="Text/Cover.xhtml" id="Cov" media-type="application/xhtml+xml" />
    <item href="Text/Back.xhtml" id="Back" media-type="application/xhtml+xml" />
  </manifest>

我已经有了此代码.请帮我.谢谢

I already have this code. Please, help me. Thank you

HtmlAgilityPack.HtmlDocument document2 = new 

HtmlAgilityPack.HtmlDocument();
document2.Load(@"C:\try.html");
HtmlNode[] nodes = document2.DocumentNode.SelectNodes("//manifest").ToArray();

foreach (HtmlNode item in nodes)
{
    Console.WriteLine(item.InnerHtml);
}

推荐答案

如果我理解正确,那么:

If I understand correctly then:

HtmlAgilityPack.HtmlDocument document2 = new HtmlAgilityPack.HtmlDocument();
document2.Load(@"C:\try.html");

string tag = document2.GetElementbyId("yourid").Name;
string href = document2.GetElementbyId("yourid").GetAttributeValue("href", "");

这篇关于HtmlAgilityPack-如何通过ID获取标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆