使用c#解析Html文档 [英] Parsing Html Document using c#

查看:576
本文介绍了使用c#解析Html文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下代码来解析html文档&将其存储为csv文件。



i have used the following code to parse html document & store it as csv file.

string actuald=null;
string data1 = File.ReadAllText("E://text.html");
//Console.WriteLine(data1);
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(data1);
HtmlNodeCollection col = doc.DocumentNode.SelectNodes("//pre");

foreach (HtmlNode node in col)
{
     actuald=node.Attributes[""].Value;
}
File.WriteAllText("E://text.csv",actuald);
Console.WriteLine("Data Converted");
Console.ReadKey();





但在html doc中我没有在任何html标签下指定类?我应该在属性[]中给出wt内容?请帮助



我的html文件看起来如何



but in the html doc i have no class specified under any of the html tags? wt content should i give in the attributes[" "]?? plz help

this how my html file looks like

<HTML><HEAD><TITLE>NCEDC_Search_Results</TITLE></HEAD><BODY>Your search parameters are:<ul>
<li>start_time=1973/01/01,00:00:00
<li>end_time=2037/01/01,00:00:00
<li>minimum_magnitude=3.0
<li>maximum_magnitude=10
<li>etype=E
<li>rflag=A,F,H,I
<li>system=selected
<li>format=ncread
</ul>
<PRE>
Date       Time             Lat       Lon  Depth   Mag Magt  Nst Gap  Clo  RMS  SRC   Event ID
----------------------------------------------------------------------------------------------
1973/01/01 06:59:19.23  36.8037 -121.5087   5.65  3.60   Md   28  35    6 0.09 NCSN    1013957
1973/01/01 07:57:39.65  37.0925 -121.5055   9.19  3.10   ML   45  90    5 0.07 NCSN    1013959
</pre></html>

推荐答案

使用 Html Agility Pack [ ^ ]用于解析(X)HTML文件。
Use Html Agility Pack[^] for parsing (X)HTML files.


使用html敏捷包你可以很容易地做到这一点。



Using html agility pack you can easily do this.

foreach (HtmlNode node in col)
{
     actuald=actualid + node.Value;
}
File.WriteAllText("E://text.csv",actuald);
Console.WriteLine("Data Converted");
Console.ReadKey();


抱歉,我有问题我找不到调用的方法(LoadHtml)你能告诉我怎么能我使用它

提前感谢:)
sorry, I have problem I can't find the method called (LoadHtml) could you tell me how can I use it
thanks in advance :)


这篇关于使用c#解析Html文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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