开发者

HTML Agility Parsing

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">&nbsp;</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&nbsp;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&nbsp;Melbourne/Dubai01 Sep17:5017:50DEPARTEDEK413&nbsp;Sydney/Dubai01 Sep18:0018:00DEPARTEDQF44&nbsp;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']");


You are using InnerText which strips out the HTML.

Use InnerHtml:

string rate = rateNode.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:

var firstRow = rateNode.SelectSingleNode("./table/tbody/tr[0]");
string origin = firstRow.SelectSingleNode("./td[@class = 'origin']");


If you want to work with linq to xml, you can transform the HtmlDocument to a xml string:

HtmlWeb web = new HtmlWeb();
HtmlAgilityPack.HtmlDocument doc = web.Load("http://www.SourceURL");  
doc.OptionOutputAsXml = true;
System.IO.StringWriter sw = new System.IO.StringWriter();
System.Xml.XmlTextWriter xw = new System.Xml.XmlTextWriter(sw);
doc.Save(xw);
string result = sw.ToString();

Then you only need create an XDocument objet and load with the xml string:

System.Xml.Linq.XDocument xDoc = System.Xml.Linq.XDocument.Parse(result);

And now you have a XDocument to play with Linq.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜