HTML敏捷解析
我想解析 HTML 表并使用 XML to LINQ 在绑定列表框中显示内容。
我正在使用 HTML Agility pack 并使用此代码。
HtmlWeb web = new HtmlWeb();
HtmlAgilityPack.HtmlDocument doc = web.Load("http://www.SourceURL");
HtmlNode rateNode = doc.DocumentNode.SelectSingleNode("//div[@id='FlightInfo_FlightInfoUpdatePanel']");
string rate = rateNode.InnerText;
this.richTextBox1.Text = rate;
HTML 看起来像这样..
<div id="FlightInfo_FlightInfoUpdatePanel">
<table cellspacing="0" cellpadding="0"><tbody>
<tr class="">
<td class="airline"><img src="/images/airline logos/NZ.gif" title="AIR NEW ZEALAND LIMITED. " alt="AIR NEW ZEALAND LIMITED. " /></td>
<td class="flight">NZ8</td>
<td class="codeshare"> </td>
<td class="origin">San Francisco</td>
<td class="date">01 Sep</td>
<td class="time">17:15</td>
<td class="est">18:00</td>
<td class="status">DEPARTED</td>
</tr>
但它返回的是 this
NZ8 San Francisco01 Sep17:1518:00DEPARTEDAC6103NZ8San Francisco01 Sep17:1518:00DEPARTEDCO6754NZ8San Francisco01 Sep17:1518:00DEPARTEDLH7157NZ8San Francisco01 Sep17:1518:00DEPARTEDUA6754NZ8San Francisco01 Sep17:1518:00DEPARTEDUS5308NZ8San Francisco01 Sep17:1518:00DEPARTEDVS7408NZ8San Francisco01 Sep17:1518:00DEPARTEDEK407 Melbourne/Dubai01 Sep17:5017:50DEPARTEDEK413 Sydney/Dubai01 Sep18:0018:00DEPARTEDQF44 Sydney01
我想要的是将其解析为 XML 格式,然后使用 LINQ to XML 将 XML 解析为绑定的列表框项源。
我想我需要为每个课程使用以下内容的变体,但需要一些帮助。
HtmlNodeCollection cols = rows[i].SelectNodes(".//td[@class='flight']");
I would like to parse an HTML table and disaply contents using XML to LINQ in an bound listbox.
I am using HTML Agility pack and using this code.
HtmlWeb web = new HtmlWeb();
HtmlAgilityPack.HtmlDocument doc = web.Load("http://www.SourceURL");
HtmlNode rateNode = doc.DocumentNode.SelectSingleNode("//div[@id='FlightInfo_FlightInfoUpdatePanel']");
string rate = rateNode.InnerText;
this.richTextBox1.Text = rate;
The HTML looks like this..
<div id="FlightInfo_FlightInfoUpdatePanel">
<table cellspacing="0" cellpadding="0"><tbody>
<tr class="">
<td class="airline"><img src="/images/airline logos/NZ.gif" title="AIR NEW ZEALAND LIMITED. " alt="AIR NEW ZEALAND LIMITED. " /></td>
<td class="flight">NZ8</td>
<td class="codeshare"> </td>
<td class="origin">San Francisco</td>
<td class="date">01 Sep</td>
<td class="time">17:15</td>
<td class="est">18:00</td>
<td class="status">DEPARTED</td>
</tr>
But it is returning this
NZ8 San Francisco01 Sep17:1518:00DEPARTEDAC6103NZ8San Francisco01 Sep17:1518:00DEPARTEDCO6754NZ8San Francisco01 Sep17:1518:00DEPARTEDLH7157NZ8San Francisco01 Sep17:1518:00DEPARTEDUA6754NZ8San Francisco01 Sep17:1518:00DEPARTEDUS5308NZ8San Francisco01 Sep17:1518:00DEPARTEDVS7408NZ8San Francisco01 Sep17:1518:00DEPARTEDEK407 Melbourne/Dubai01 Sep17:5017:50DEPARTEDEK413 Sydney/Dubai01 Sep18:0018:00DEPARTEDQF44 Sydney01
What I would like is pasrse this to XML format and then use LINQ to XML to parse the XML to a bound listbox itemsource.
I am thinking I need to use a variation of the below for each class, but would like some help.
HtmlNodeCollection cols = rows[i].SelectNodes(".//td[@class='flight']");
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您正在使用
InnerText
来删除 HTML。使用
InnerHtml
:您可以从此字符串创建 XML 文档(假设它是有效的 XML)。
您还可以按照检索
rateNode
的相同方式查询它 - 选择其子节点:You are using
InnerText
which strips out the HTML.Use
InnerHtml
:You can create an XML document from this string (assuming it is valid XML).
You can also query the
rateNode
in the same way you retrieved it - selecting its child nodes:如果您想使用 linq to xml,您可以将 HtmlDocument 转换为 xml 字符串:
然后您只需创建一个 XDocument 对象并加载 xml 字符串:
现在您就有了一个可以使用 Linq 的 XDocument。
If you want to work with linq to xml, you can transform the HtmlDocument to a xml string:
Then you only need create an XDocument objet and load with the xml string:
And now you have a XDocument to play with Linq.