{"id":961,"date":"2011-12-10T10:54:12","date_gmt":"2011-12-10T02:54:12","guid":{"rendered":"http:\/\/learn-house.idv.tw\/?p=961"},"modified":"2012-06-17T13:04:21","modified_gmt":"2012-06-17T05:04:21","slug":"wp7%e5%9c%a8phone7%e5%a6%82%e4%bd%95%e5%af%a6%e4%bd%9chtml-parser","status":"publish","type":"post","link":"https:\/\/learn-house.idv.tw\/?p=961","title":{"rendered":"[WP7]\u5728Phone7\u5982\u4f55\u5be6\u4f5cHTML Parser"},"content":{"rendered":"<p>\u7531\u65bc\u5ba2\u6236\u7684\u95dc\u4fc2\u8b93\u5fae\u8edf\u8f3e\u8f49\u627e\u4e0a\u6211\u5011\uff0c\u539f\u4f86\u5fae\u8edf\u6700\u8fd1\u525b\u63a8\u8292\u679c(Mango)\u6a5f\uff0c\u6240\u4ee5\u5e0c\u671b\u6709\u5927\u91cf\u4f7f\u7528\u8005\u7684APP\u5ee0\u5546\u80fd\u4e5f\u63d0\u4f9bPhone7\u7684\u7248\u672c<\/p>\n<p>\u53c8\u525b\u597d\u6211\u9032\u516c\u53f8\u5c31\u662f\u60f3\u81f4\u529b\u65bc\u667a\u6167\u624b\u6a5f\u7684\u958b\u767c\uff0c\u56e0\u6b64\u4e3b\u7ba1\u5c31\u6d3e\u6211\u53bb\u6e2c\u8a66\u9019\u500b\u65b0\u6280\u8853<\/p>\n<p>\u6211\u82b1\u534a\u500b\u6708\u7684\u6642\u9593\u908a\u6e2c\u8a66\u908a\u5c07\u958b\u51fa\u7684\u529f\u80fd\u898f\u683c\u5be6\u4f5c\u51fa\u4f86\uff0c\u518d\u4f86\u7684\u534a\u500b\u6708\u5c31\u770b\u80fd\u4e0d\u80fd\u5c07GUI\u7684\u8a2d\u8a08\u7f8e\u5316<\/p>\n<p>\u8001\u5be6\u8aaa\u6211\u5ff5\u66f8\u7684\u6642\u5019\u53ea\u6703\u5bebNative Code(C\/C++)\uff0c\u53cd\u800c\u6211\u9019\u5e74\u7d00\u7684\u5e74\u8f15\u4eba\u61c9\u8a72\u6703\u6bd4\u8f03\u719f\u7684Managed code(Java, C#)\u6211\u771f\u7684\u6c92\u6709\u4ec0\u9ebc\u7d93\u9a57<\/p>\n<p>\u9019\u500b\u534a\u6708\u4e0b\u4f86\u6211\u5728Phoe7\u7814\u7a76\u51fa\u4e86\u5f88\u591a\u5fc3\u5f97\uff0c\u4e5f\u662f\u7b2c\u4e00\u6b21\u5bebC#\u7684\u7a0b\u5f0f<\/p>\n<p>\u6709\u7a7a\u7684\u8a71\u6211\u518d\u4e00\u4e00\u7684\u628a\u4e00\u4e9bPhone7\u7684\u958b\u767c\u5fc3\u5f97\u8207\u6559\u5b78\u767c\u8868\u4e0a\u4f86\uff0c\u8b93\u5165\u9580\u7684\u4eba\u4e0d\u7528\u518d\u91cd\u65b0\u8d70\u6211\u8d70\u904e\u7684\u51a4\u6789\u8def<!--more-->\u5728C#\u60f3\u8981\u505a\u5230HTML Parsing\u62dc\u898b\u4f30\u72d7\u6703\u767c\u73fe\u5927\u5bb6\u90fd\u6703\u6307\u5411\u4f7f\u7528\u7b2c\u4e09\u65b9\u7684\u5957\u4ef6<a href=\"http:\/\/htmlagilitypack.codeplex.com\" target=\"_blank\">HTML Agility Pack<\/a><\/p>\n<p>\u5f88\u7121\u5948\u7684\u6307\u63d0\u4f9bPC\u4e0a\u7684\u7248\u672c\uff0c\u4e0d\u904e\u5f8c\u4f86\u5728\u8a72\u5b98\u65b9\u7684<a href=\"http:\/\/htmlagilitypack.codeplex.com\/discussions\/228589\" target=\"_blank\">\u8a0e\u8ad6\u5340<\/a>\u5f97\u77e5\u539f\u4f86\u4e5f\u6709\u63d0\u4f9b<a href=\"http:\/\/htmlagilitypack.codeplex.com\/SourceControl\/list\/changesetseControl\/list\/changesets\" target=\"_blank\">Phone7\u7684\u7248\u672c<\/a><\/p>\n<p>\u4e0b\u8f09\u7684SourceCode\u4e2dTrunk\u8cc7\u6599\u593e\u5167\u5c31\u6709Phone7\u7684\u5c08\u6848(HAPPhone.sln)\uff0c\u81ea\u5df1\u7de8\u8b6f\u6210dll\u6216\u76f4\u63a5\u532f\u5165\u7a0b\u5f0f\u5c31\u53ef\u4ee5\u4f7f\u7528<\/p>\n<p>\u5728\u7528\u6cd5\u4e0a\u4e5f\u8b93\u6211\u5403\u76e1\u82e6\u982d\uff0c\u56e0\u70ba\u4ed6\u7684\u5beb\u6cd5\u5f88\u5947\u602a\uff0c\u7528\u6cd5\u66f4\u662f\u5947\u602a\uff0c\u4e0d\u77e5\u9053\u662f\u4e0d\u662f\u56e0\u70ba\u6211\u771f\u6c92\u4ec0\u9ebc\u5bebmanaged cdoe\u7d93\u9a57\u7684\u95dc\u4fc2<\/p>\n<p>\u800c\u4e14<span style=\"color: #ff0000;\"><strong>\u53ea\u80fd\u5403\u8981parsing\u7684HTML\u7db2\u5740(Uri)<\/strong><\/span>\uff0c\u96d6\u7136\u5f8c\u4f86\u6211\u6709\u53bb\u6539SourceCode\u8b93\u5b83\u4e5f\u80fd\u5403HTML\u7684\u6587\u5b57\u6a94\uff0c\u4f46\u6c92\u6709\u6539\u5f97\u5f88\u6f02\u4eae<\/p>\n<p>\u6709\u9700\u8981\u7684\u8a71\u6211\u5728\u8ff4\u97ff\u4e2d\u518d\u5206\u4eab\u6211\u4fee\u6539\u7684\u4e0d\u6210\u719f\u534a\u6210\u54c1\u6539\u6cd5\u5427!!<\/p>\n<p>\u9996\u5148\u5148\u8b1b\u4f7f\u7528\u65b9\u5f0f\uff0c\u57fa\u672c\u7684\u532f\u5165dll\u8ddfdll\u7684\u547c\u53eb\u7528\u6cd5\u6211\u5c31\u4e0d\u8a73\u8ff0\u4e86\uff0c\u9019\u500b\u61c9\u8a72\u4f30\u72d7\u4e00\u4e0b\u5c31\u6703\u6709\u7b54\u6848<\/p>\n<p>\u6839\u64da\u5b98\u65b9\u7684\u5c08\u6848\u770b\u4f86\u53ea\u80fd\u7528\u4e0b\u9762\u7684\u5beb\u6cd5\u4f86\u547c\u53eb<\/p>\n<p>[c]<br \/>\nHtmlWeb.LoadAsync(&quot;http:\/\/www.google.com&quot;, (s, args) =&gt;<br \/>\n{<br \/>\nResults.Text = String.Join(Environment.NewLine,<br \/>\nargs.Document.DocumentNode.Descendants(&quot;a&quot;).<br \/>\nSelect(<br \/>\nx =&gt;<br \/>\nx.GetAttributeValue(&quot;href&quot;, &quot;&quot;)).ToArray());<br \/>\n});<br \/>\n[\/c]<\/p>\n<pre>Parsing\u7684\u7528\u6cd5\u662f\u5148\u5728args.Document.DocumentNode.Descendants(\"a\")\u6307\u5b9a\u8981\u64f7\u53d6\u7684Tag Element\r\n\u518d\u4f86\u5c31\u662f\u53d6\u51faTag\u7684\u5c6c\u6027\u503cx.GetAttributeValue(\"href\", \"\")).ToArray());<\/pre>\n<p>\u4e0a\u9762\u5b98\u65b9\u7684\u7bc4\u4f8b\u5c31\u662f\u6703\u53d6\u51fa\u6a19\u7c64\u70ba<\/p>\n<p>[html]&lt;a href=&quot;http:\/\/www.google.com&quot;&gt;[\/html]<\/p>\n<p>\u4e2d\u7684http:\/\/www.google.com<\/p>\n<p>\u800c\u5982\u679c\u8981\u5728<\/p>\n<p>[html]&lt;li&gt;&lt;a href=&quot;http:\/\/learn-house.idv.tw\/?p=961&quot; title=&quot;[WP7]\u5728Phone7\u5982\u4f55\u5be6\u4f5cHTML Parser&quot;&gt;[\/html]<\/p>\n<p>\u53d6\u51fa<span style=\"color: #008000;\"><strong>[WP7]\u5728Phone7\u5982\u4f55\u5be6\u4f5cHTML Parser<\/strong><\/span>\u5c31\u53ea\u8981\u5206\u5225\u4fee\u6539args.Document.DocumentNode.Descendants(&#8220;<span style=\"color: #ff0000;\">a<\/span>&#8220;)\u8207x.GetAttributeValue(&#8220;<span style=\"color: #ff0000;\">title<\/span>&#8220;, &#8220;&#8221;)).ToArray());<\/p>\n<p>\u8981\u7279\u5225\u6ce8\u610f\u7684\u662f\uff0c\u5c31\u7b97HTML\u7684Tag\u662f\u5927\u5beb\u5b57\u6bcd\uff0c\u4e5f\u4e00\u5b9a\u8981\u5c0f\u5beb\u4f86\u53d6\u51fa\uff0c\u4e0d\u7136\u6703\u53d6\u4e0d\u5230\u503c<\/p>\n<p>\u518d\u4f86\u5c31\u662f\u53d6\u51fa\u7684\u503c\u8981\u600e\u9ebc\u4e1f\u51fa\u4f86\u4f7f\u7528\uff0c\u56e0\u70ba\u53d6\u5230\u7684\u503c\u5145\u5176\u91cf\u53ea\u662f\u500b\u5340\u57df\u8b8a\u6578\uff0c\u6839\u672c\u4e1f\u4e0d\u51fa\u4f86\u4f7f\u7528<\/p>\n<p>\u6240\u4ee5\u9019\u500b\u7bc4\u4f8bCode\u7528Results.Text\u4f86\u63a5\u9019\u500b\u7d50\u679c\u503c\uff0cResults.Text\u662f\u986f\u793a\u6587\u5b57\u7684\u63a7\u5236\u9805(TextBlock)<\/p>\n<p>\u4f46\u9019\u7528\u6cd5\u4e0d\u77e5\u9053\u70ba\u4ec0\u9ebc\uff0c\u5982\u679c\u6709\u5728\u5f8c\u7e8c\u9032\u884c\u8655\u7406\u7684\u7a0b\u5f0f\u78bc\uff0c\u547c\u53ebParsing\u7684\u90a3\u6bb5\u7a0b\u5f0f\u78bc\u5c31\u6703\u5931\u6548\uff0c\u7a0b\u5f0f\u90fd\u4e0d\u6703\u8dd1\u9032\u53bb<\/p>\n<p>\u90a3\u9019\u6a23\u4e1f\u51fa\u4f86\u53ea\u80fd\u986f\u793a\uff0c\u4e0d\u80fd\u505a\u4efb\u4f55\u52d5\u4f5c\uff0c\u90a3\u53c8\u6709\u4ec0\u9ebc\u610f\u7fa9 = =&#8221;<\/p>\n<p>\u5728\u82e6\u601d\u4e0d\u89e3\u7684\u60c5\u6cc1\u4e0b\uff0c\u6211\u662f\u7528\u4e86\u4e00\u7a2e\u4e1f\u51fa\u503c\u7684\u65b9\u6cd5\uff0c\u610f\u5916\u767c\u73fe\u53ef\u4ee5\u9019\u6a23\u4f7f\u7528<\/p>\n<p>[c]<br \/>\n HtmlWeb.LoadAsync(&quot;http:\/\/www.google.com&quot;, (s, args) =&gt;<br \/>\n {<br \/>\n Results.Text = String.Join(Environment.NewLine,<br \/>\n args.Document.DocumentNode.Descendants(&quot;a&quot;).<br \/>\n Select(<br \/>\n x =&gt;<br \/>\n x.GetAttributeValue(&quot;href&quot;, &quot;&quot;)).ToArray());<br \/>\n });<\/p>\n<p>Action act = new Action(OutputResult);<br \/>\n this.Dispatcher.BeginInvoke(act, outputString);<\/p>\n<p>[\/c]<\/p>\n<p>\u7b2c10,11\u884c\u7a0b\u5f0f\u78bc\u5c31\u662f\u628a\u503c\u4e1f\u51fa\u4f86\uff0c\u6211\u5011\u518d\u5beb\u4e00\u500bfunction(\u4e0a\u9762\u6307\u5b9aOutputResult\u70ba\u63a5\u6536\u7684callback function)\u4f86\u63a5\u6536\u9019\u500b\u503c\uff0coutputString\u5373\u70ba\u4e1f\u51fa\u7684\u503c\uff0c\u9032\u4e00\u6b65\u518d\u4f86\u5c0d\u503c\u505a\u8655\u7406<\/p>\n<p>[c]void OutputResult(string outputString)<br \/>\n {<br \/>\n StringReader reader = new StringReader(outputString);<br \/>\n string line;<br \/>\n int i = 0;<br \/>\n while ((line = reader.ReadLine()) != null)<br \/>\n {<br \/>\n parsingResult[i] = line;<br \/>\n i++;<br \/>\n }<br \/>\n }[\/c]<\/p>\n<pre>parsingResult\u662f\u5b57\u4e32\u9663\u5217\uff0c\u6211\u7528\u5b83\u4f86\u5132\u5b58\u63a5\u6536\u7d50\u679c\u503c<\/pre>\n<p>\u900f\u904eparsingResult\uff0c\u9019\u6a23\u5c31\u80fd\u958b\u5fc3\u7684\u4f7f\u7528Parsing\u5f8c\u7684\u7d50\u679c\u5566~~~<\/p>\n<div id=\"_mcePaste\" style=\"position: absolute; left: -10000px; top: 578px; width: 1px; height: 1px; overflow: hidden;\">\n<pre>[\/html]<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u7531\u65bc\u5ba2\u6236\u7684\u95dc\u4fc2\u8b93\u5fae\u8edf\u8f3e\u8f49\u627e\u4e0a\u6211\u5011\uff0c\u539f\u4f86\u5fae\u8edf\u6700\u8fd1\u525b\u63a8\u8292\u679c(Mango)\u6a5f\uff0c\u6240\u4ee5\u5e0c\u671b\u6709\u5927\u91cf\u4f7f\u7528\u8005\u7684APP\u5ee0\u5546\u80fd\u4e5f\u63d0<span class=\"post-excerpt-end\">&hellip;<\/span><\/p>\n<p class=\"more-link\"><a href=\"https:\/\/learn-house.idv.tw\/?p=961\" class=\"themebutton\">Read More<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-961","post","type-post","status-publish","format-standard","hentry","category-5"],"_links":{"self":[{"href":"https:\/\/learn-house.idv.tw\/index.php?rest_route=\/wp\/v2\/posts\/961","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/learn-house.idv.tw\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/learn-house.idv.tw\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/learn-house.idv.tw\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/learn-house.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=961"}],"version-history":[{"count":0,"href":"https:\/\/learn-house.idv.tw\/index.php?rest_route=\/wp\/v2\/posts\/961\/revisions"}],"wp:attachment":[{"href":"https:\/\/learn-house.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=961"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learn-house.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=961"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learn-house.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=961"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}