简体   繁体   中英

Extract id style from html page using Html agility pack

I have ac# application. I need to extract data from a html page and add it to my database. The html page contains some css code and I am interested in all of the id's attributes from the css. How can I pull out the id's info into my code? I tried something like this but it doesn't seem to work:

var styles = document.DocumentNode.SelecNodes("//style");
foreach(HtmlNode node in styles)
{
   var text = node.Attributes["id"];
}

I really appreciate any help!

Try this, access Id property directly :

var styles = document.DocumentNode.SelecNodes("//*[@style]");
foreach(HtmlNode node in styles)
{
   var text = node.Id;
}

Edit: expression changed to "//*[@style]" which gets you only elements with style attribute.

More of a fishing rod than a fish, but that's all I got time to do ATM.

First, look at this tutorial: xpath on w3schools . I've done some work with XPath, and it was only after going through their tutorial that things started to make a bit of sense.

Then, please get this html agility test pack , it will let you quickly test your code against the page you're trying to parse.

From here, it should be a short way to get what you want.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM