我正在加载来自this Google News URL的XML:
结果是:
<rss version="2.0"><channel><generator>NFE/1.0</generator><title>"birch" - Google News</title><link>https://news.google.com/news?hl=en&pz=1&ned=uk&q=%5Cx22birch%5Cx22</link><language>en-GB</language><webMaster>news-feedback@google.com</webMaster><copyright>&copy;2017 Google</copyright><pubDate>Sun, 09 Jul 2017 15:30:02 GMT</pubDate><lastBuildDate>Sun, 09 Jul 2017 15:30:02 GMT</lastBuildDate><image><title>"birch" - Google News</title><url>https://ssl.gstatic.com/news-static/img/logo/en_uk/news.gif</url><link>https://news.google.com/news?hl=en&pz=1&ned=uk&q=%5Cx22birch%5Cx22</link></image><item><title>Why Birch Water Is The New Beauty Ingredient You Need To Know - Refinery29</title><link>http://news.google.com/news/url?sa=t&fd=R&ct2=uk&usg=AFQjCNFVyvekSpSA-H1CkHd74v6_mAs60w&clid=c3a7d30bb8a4878e06b80cf16b898331&ei=-UtiWbGZHIrkhAGMmrDYBw&url=http://www.refinery29.uk/birch-water-beauty-benefits</link><guid isPermaLink="false">tag:news.google.com,2005:cluster=http://www.refinery29.uk/birch-water-beauty-benefits</guid><pubDate>Wed, 05 Jul 2017 07:09:16 GMT</pubDate><description><table border="0" cellpadding="2" cellspacing="7" style="vertical-align:top;"><tr><td width="80" align="center" valign="top"><font style="font-size:85%;font-family:arial,sans-serif"><a href="http://news.google.com/news/url?sa=t&amp;fd=R&amp;ct2=uk&amp;usg=AFQjCNFVyvekSpSA-H1CkHd74v6_mAs60w&amp;clid=c3a7d30bb8a4878e06b80cf16b898331&amp;ei=-UtiWbGZHIrkhAGMmrDYBw&amp;url=http://www.refinery29.uk/birch-water-beauty-benefits"><img src="//t0.gstatic.com/images?q=tbn:ANd9GcRnLZw3tqOQnwtqioj-xl56DSiweOwGyk-lvEwAt8xipD9O6bw5agzjJi1OO58TfM1UY2z9d9V2" alt="" border="1" width="80" height="80"><br><font size="-2">Refinery29</font></a></font></td><td valign="top" class="j"><font style="font-size:85%;font-family:arial,sans-serif"><br><div style="padding-top:0.8em;"><img alt="" height="1" width="1"></div><div class="lh"><a href="http://news.google.com/news/url?sa=t&amp;fd=R&amp;ct2=uk&amp;usg=AFQjCNFVyvekSpSA-H1CkHd74v6_mAs60w&amp;clid=c3a7d30bb8a4878e06b80cf16b898331&amp;ei=-UtiWbGZHIrkhAGMmrDYBw&amp;url=http://www.refinery29.uk/birch-water-beauty-benefits"><b>Why <b>Birch</b> Water Is The New Beauty Ingredient You Need To Know</b></a><br><font size="-1"><b><font color="#6f6f6f">Refinery29</font></b></font><br><font size="-1">Not just the source of one of the more questionable new wellness drinks, <b>birch</b> trees are also trending in the world of skincare. Often referred to as the tree of life, <b>birch</b> is found across northern Europe, particularly in Scandinavia, and has been <b>...</b></font><br><font size="-1" class="p"></font><br><font class="p" size="-1"><a class="p" href="http://news.google.com/news/more?ncl=dSsgf_hNiR1IBhM&amp;authuser=0&amp;ned=uk"><nobr><b></b></nobr></a></font></div></font></td></tr></table></description></item><item><title>Recycling Lives' takeover of scrap metal site L&D Mortimer site on ... - Bury Times</title><link>http://news.google.com/news/url?sa=t&fd=R&ct2=uk&usg=AFQjCNH7tJ4QbojCmGT5ZZo29j5upz0xeQ&clid=c3a7d30bb8a4878e06b80cf16b898331&cid=52779549822946&ei=-UtiWbGZHIrkhAGMmrDYBw&url=http://www.burytimes.co.uk/news/15396732.Takeover_of_scrap_metal_site_will_save_and_create_jobs/</link><guid isPermaLink="false">tag:news.google.com,2005:cluster=http://www.burytimes.co.uk/news/15396732.Takeover_of_scrap_metal_site_will_save_and_create_jobs/</guid><pubDate>Sat, 08 Jul 2017 06:03:26 GMT</pubDate><description><table border="0" cellpadding="2" cellspacing="7" style="vertical-align:top;"><tr><td width="80" align="center" valign="top"><font style="font-size:85%;font-family:arial,sans-serif"><a href="http://news.google.com/news/url?sa=t&amp;fd=R&amp;ct2=uk&amp;usg=AFQjCNH7tJ4QbojCmGT5ZZo29j5upz0xeQ&amp;clid=c3a7d30bb8a4878e06b80cf16b898331&amp;cid=52779549822946&amp;ei=-UtiWbGZHIrkhAGMmrDYBw&amp;url=http://www.burytimes.co.uk/news/15396732.Takeover_of_scrap_metal_site_will_save_and_create_jobs/"><img src="//t3.gstatic.com/images?q=tbn:ANd9GcRLzihFeSA1g11dR0H1T3v3nuWT7pQ0ss7urR_21uO0AhuW23gHhA4nTUMo_vZaAgAZ7j9L5Nk-" alt="" border="1" width="80" height="80"><br><font size="-2">Bury Times</font></a></font></td><td valign="top" class="j"><font style="font-size:85%;font-family:arial,sans-serif"><br><div style="padding-top:0.8em;"><img alt="" height="1" width="1"></div><div class="lh"><a href="http://news.google.com/news/url?sa=t&amp;fd=R&amp;ct2=uk&amp;usg=AFQjCNH7tJ4QbojCmGT5ZZo29j5upz0xeQ&amp;clid=c3a7d30bb8a4878e06b80cf16b898331&amp;cid=52779549822946&amp;ei=-UtiWbGZHIrkhAGMmrDYBw&amp;url=http://www.burytimes.co.uk/news/15396732.Takeover_of_scrap_metal_site_will_save_and_create_jobs/"><b>Recycling Lives&#39; takeover of scrap metal site L&amp;D Mortimer site on ...</b></a><br><font size="-1"><b><font color="#6f6f6f">Bury Times</font></b></font><br><font size="-1">AN award-winning social business has moved in – both saving and creating jobs.</font><br><font size="-1" class="p"></font><br><font class="p" size="-1"><a class="p" href="http://news.google.com/news/more?ncl=d-Kqhhcau4k19TM&amp;authuser=0&amp;ned=uk"><nobr><b>and more&nbsp;&raquo;</b></nobr></a></font></div></font></td></tr></table></description></item><item><title>Bacteria closes Dartmouth's Birch Cove Beach | The Chronicle Herald - TheChronicleHerald.ca</title><link>http://news.google.com/news/url?sa=t&fd=R&ct2=uk&usg=AFQjCNHidwNRYJY0vVolosZbesiAkOMpSQ&clid=c3a7d30bb8a4878e06b80cf16b898331&cid=52779548150492&ei=-UtiWbGZHIrkhAGMmrDYBw&url=http://thechronicleherald.ca/metro/1483620-bacteria-closes-dartmouth%25E2%2580%2599s-birch-cove-beach</link><guid isPermaLink="false">tag:news.google.com,2005:cluster=http://thechronicleherald.ca/metro/1483620-bacteria-closes-dartmouth%E2%80%99s-birch-cove-beach</guid><pubDate>Wed, 05 Jul 2017 15:26:00 GMT</pubDate><description><table border="0" cellpadding="2" cellspacing="7" style="vertical-align:top;"><tr><td width="80" align="center" valign="top"><font style="font-size:85%;font-family:arial,sans-serif"><a href="http://news.google.com/news/url?sa=t&amp;fd=R&amp;ct2=uk&amp;usg=AFQjCNGMvNI3ZSUFTfUWD6ia0_HcE3gz-A&amp;clid=c3a7d30bb8a4878e06b80cf16b898331&amp;cid=52779548150492&amp;ei=-UtiWbGZHIrkhAGMmrDYBw&amp;url=http://www.metronews.ca/news/halifax/2017/07/05/dartmouth-beach-closed-swimming-high-bacteria.html"><img src="//t3.gstatic.com/images?q=tbn:ANd9GcSuM4Lbx1eh7UOr69MB6KAkq5WMI-e3z6loUwqIBzCB_soiJly7oj33gwYTcB8F9QmZvKnZ6EqM" alt="" border="1" width="80" height="80"><br><font size="-2">MetroNews Canada</font></a></font></td><td valign="top" class="j"><font style="font-size:85%;font-family:arial,sans-serif"><br><div style="padding-top:0.8em;"><img alt="" height="1" width="1"></div><div class="lh"><a href="http://news.google.com/news/url?sa=t&amp;fd=R&amp;ct2=uk&amp;usg=AFQjCNHidwNRYJY0vVolosZbesiAkOMpSQ&amp;clid=c3a7d30bb8a4878e06b80cf16b898331&amp;cid=52779548150492&amp;ei=-UtiWbGZHIrkhAGMmrDYBw&amp;url=http://thechronicleherald.ca/metro/1483620-bacteria-closes-dartmouth%25E2%2580%2599s-birch-cove-beach"><b>Bacteria closes Dartmouth&#39;s Birch Cove Beach | The Chronicle Herald</b></a><br><font size="-1"><b><font color="#6f6f6f">TheChronicleHerald.ca</font></b></font><br><font size="-1">Birch Cove Beach is closed due to high bacteria levels in the water. Halifax Regional Municipality is advising residents not to swim at the beach until further&nbsp;...</font><br><font size="-1"><a href="http://news.google.com/news/url?sa=t&amp;fd=R&amp;ct2=uk&amp;usg=AFQjCNGMvNI3ZSUFTfUWD6ia0_HcE3gz-A&amp;clid=c3a7d30bb8a4878e06b80cf16b898331&amp;cid=52779548150492&amp;ei=-UtiWbGZHIrkhAGMmrDYBw&amp;url=http://www.metronews.ca/news/halifax/2017/07/05/dartmouth-beach-closed-swimming-high-bacteria.html">Dartmouth beach closed to swimming over high bacteria levels ...</a><font size="-1" color="#6f6f6f"><nobr>MetroNews Canada</nobr></font></font><br><font size="-1" class="p"></font><br><font class="p" size="-1"><a class="p" href="http://news.google.com/news/more?ncl=dvqw0clrahi5WqMQEdCYeTwyz0taM&amp;authuser=0&amp;ned=uk"><nobr><b>all 2 news articles&nbsp;&raquo;</b></nobr></a></font></div></font></td></tr></table></description></item><description>Google News</description></channel></rss>
我想提取每个项目。
string url = "https://news.google.com/news?pz=1&cf=all&ned=uk&hl=en&q=/"birch/"&cf=all&output=rss"
XmlDocument doc = new XmlDocument();
doc.Load(url);
XmlElement root = doc.DocumentElement;
XmlNodeList nodes = root.SelectNodes("item")
foreach (XmlNode node in nodes)
{
// do stuff
}
但我似乎无法访问每个项目的网址?
有什么建议吗?
答案 0 :(得分:1)
您可以使用System.Xml.Linq
以下代码对我有用。
XDocument xDoc = XDocument.Load(@"Filepath/XMLFile1.xml");
List<XElement> LXElement = xDoc.Root.Element("channel").Elements("item").ToList();
foreach (XElement iElement in LXElement)
{
Console.WriteLine(iElement.Element("title").Value);
Console.WriteLine(iElement.Element("link").Value);
Console.WriteLine(iElement.Element("guid").Value);
Console.WriteLine(iElement.Element("pubDate").Value);
Console.WriteLine(iElement.Element("description").Value);
}