开发者

How to read xpath values from many HTML files in .Net?

I have about 5000 html files in a folder. I need to loop through them, open, grab say 10 values using xpath, close, and store in (SQL Server) DB.

What is the easiest way to do read the xpath v开发者_Python百科alues using .Net?

The xpaths should be pretty stable.

Please provide example code to read one value, say /html/head/title/text()

Thanks


I think you should look into the HTML Agility Pack. It is an HTML parser rather than an XML parser, and is better for this task. If there is anything that doesn't agree with the XML being parsed then the parser will throw and exception. Using an HTML parser gives you a bit more leeway with the input files.

Example showing how to do something with all HREF (link) attributes:

 HtmlDocument doc = new HtmlDocument();
 doc.Load("file.htm");
 foreach(HtmlNode link in doc.DocumentElement.SelectNodes("//a[@href"])
 {
    HtmlAttribute att = link["href"];
    att.Value = FixLink(att);
 }

I'm not near a compiler but the example you want is something like:

string title = doc.DocumentNode.SelectSingleNode("//title").InnerText;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜