如何从此XML结果中解析单个项目

时间:2017-07-09 16:22:52

标签: c# xml

我正在加载来自this Google News URL的XML:

结果是:

<rss version="2.0"><channel><generator>NFE/1.0</generator><title>&quot;birch&quot; - Google News</title><link>https://news.google.com/news?hl=en&amp;pz=1&amp;ned=uk&amp;q=%5Cx22birch%5Cx22</link><language>en-GB</language><webMaster>news-feedback@google.com</webMaster><copyright>&amp;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>&quot;birch&quot; - 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&amp;pz=1&amp;ned=uk&amp;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&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</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>&lt;table border=&quot;0&quot; cellpadding=&quot;2&quot; cellspacing=&quot;7&quot; style=&quot;vertical-align:top;&quot;&gt;&lt;tr&gt;&lt;td width=&quot;80&quot; align=&quot;center&quot; valign=&quot;top&quot;&gt;&lt;font style=&quot;font-size:85%;font-family:arial,sans-serif&quot;&gt;&lt;a href=&quot;http://news.google.com/news/url?sa=t&amp;amp;fd=R&amp;amp;ct2=uk&amp;amp;usg=AFQjCNFVyvekSpSA-H1CkHd74v6_mAs60w&amp;amp;clid=c3a7d30bb8a4878e06b80cf16b898331&amp;amp;ei=-UtiWbGZHIrkhAGMmrDYBw&amp;amp;url=http://www.refinery29.uk/birch-water-beauty-benefits&quot;&gt;&lt;img src=&quot;//t0.gstatic.com/images?q=tbn:ANd9GcRnLZw3tqOQnwtqioj-xl56DSiweOwGyk-lvEwAt8xipD9O6bw5agzjJi1OO58TfM1UY2z9d9V2&quot; alt=&quot;&quot; border=&quot;1&quot; width=&quot;80&quot; height=&quot;80&quot;&gt;&lt;br&gt;&lt;font size=&quot;-2&quot;&gt;Refinery29&lt;/font&gt;&lt;/a&gt;&lt;/font&gt;&lt;/td&gt;&lt;td valign=&quot;top&quot; class=&quot;j&quot;&gt;&lt;font style=&quot;font-size:85%;font-family:arial,sans-serif&quot;&gt;&lt;br&gt;&lt;div style=&quot;padding-top:0.8em;&quot;&gt;&lt;img alt=&quot;&quot; height=&quot;1&quot; width=&quot;1&quot;&gt;&lt;/div&gt;&lt;div class=&quot;lh&quot;&gt;&lt;a href=&quot;http://news.google.com/news/url?sa=t&amp;amp;fd=R&amp;amp;ct2=uk&amp;amp;usg=AFQjCNFVyvekSpSA-H1CkHd74v6_mAs60w&amp;amp;clid=c3a7d30bb8a4878e06b80cf16b898331&amp;amp;ei=-UtiWbGZHIrkhAGMmrDYBw&amp;amp;url=http://www.refinery29.uk/birch-water-beauty-benefits&quot;&gt;&lt;b&gt;Why &lt;b&gt;Birch&lt;/b&gt; Water Is The New Beauty Ingredient You Need To Know&lt;/b&gt;&lt;/a&gt;&lt;br&gt;&lt;font size=&quot;-1&quot;&gt;&lt;b&gt;&lt;font color=&quot;#6f6f6f&quot;&gt;Refinery29&lt;/font&gt;&lt;/b&gt;&lt;/font&gt;&lt;br&gt;&lt;font size=&quot;-1&quot;&gt;Not just the source of one of the more questionable new wellness drinks, &lt;b&gt;birch&lt;/b&gt; trees are also trending in the world of skincare. Often referred to as the tree of life, &lt;b&gt;birch&lt;/b&gt; is found across northern Europe, particularly in Scandinavia, and has been &lt;b&gt;...&lt;/b&gt;&lt;/font&gt;&lt;br&gt;&lt;font size=&quot;-1&quot; class=&quot;p&quot;&gt;&lt;/font&gt;&lt;br&gt;&lt;font class=&quot;p&quot; size=&quot;-1&quot;&gt;&lt;a class=&quot;p&quot; href=&quot;http://news.google.com/news/more?ncl=dSsgf_hNiR1IBhM&amp;amp;authuser=0&amp;amp;ned=uk&quot;&gt;&lt;nobr&gt;&lt;b&gt;&lt;/b&gt;&lt;/nobr&gt;&lt;/a&gt;&lt;/font&gt;&lt;/div&gt;&lt;/font&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;</description></item><item><title>Recycling Lives&apos; takeover of scrap metal site L&amp;D Mortimer site on ... - Bury Times</title><link>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/</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>&lt;table border=&quot;0&quot; cellpadding=&quot;2&quot; cellspacing=&quot;7&quot; style=&quot;vertical-align:top;&quot;&gt;&lt;tr&gt;&lt;td width=&quot;80&quot; align=&quot;center&quot; valign=&quot;top&quot;&gt;&lt;font style=&quot;font-size:85%;font-family:arial,sans-serif&quot;&gt;&lt;a href=&quot;http://news.google.com/news/url?sa=t&amp;amp;fd=R&amp;amp;ct2=uk&amp;amp;usg=AFQjCNH7tJ4QbojCmGT5ZZo29j5upz0xeQ&amp;amp;clid=c3a7d30bb8a4878e06b80cf16b898331&amp;amp;cid=52779549822946&amp;amp;ei=-UtiWbGZHIrkhAGMmrDYBw&amp;amp;url=http://www.burytimes.co.uk/news/15396732.Takeover_of_scrap_metal_site_will_save_and_create_jobs/&quot;&gt;&lt;img src=&quot;//t3.gstatic.com/images?q=tbn:ANd9GcRLzihFeSA1g11dR0H1T3v3nuWT7pQ0ss7urR_21uO0AhuW23gHhA4nTUMo_vZaAgAZ7j9L5Nk-&quot; alt=&quot;&quot; border=&quot;1&quot; width=&quot;80&quot; height=&quot;80&quot;&gt;&lt;br&gt;&lt;font size=&quot;-2&quot;&gt;Bury Times&lt;/font&gt;&lt;/a&gt;&lt;/font&gt;&lt;/td&gt;&lt;td valign=&quot;top&quot; class=&quot;j&quot;&gt;&lt;font style=&quot;font-size:85%;font-family:arial,sans-serif&quot;&gt;&lt;br&gt;&lt;div style=&quot;padding-top:0.8em;&quot;&gt;&lt;img alt=&quot;&quot; height=&quot;1&quot; width=&quot;1&quot;&gt;&lt;/div&gt;&lt;div class=&quot;lh&quot;&gt;&lt;a href=&quot;http://news.google.com/news/url?sa=t&amp;amp;fd=R&amp;amp;ct2=uk&amp;amp;usg=AFQjCNH7tJ4QbojCmGT5ZZo29j5upz0xeQ&amp;amp;clid=c3a7d30bb8a4878e06b80cf16b898331&amp;amp;cid=52779549822946&amp;amp;ei=-UtiWbGZHIrkhAGMmrDYBw&amp;amp;url=http://www.burytimes.co.uk/news/15396732.Takeover_of_scrap_metal_site_will_save_and_create_jobs/&quot;&gt;&lt;b&gt;Recycling Lives&amp;#39; takeover of scrap metal site L&amp;amp;D Mortimer site on ...&lt;/b&gt;&lt;/a&gt;&lt;br&gt;&lt;font size=&quot;-1&quot;&gt;&lt;b&gt;&lt;font color=&quot;#6f6f6f&quot;&gt;Bury Times&lt;/font&gt;&lt;/b&gt;&lt;/font&gt;&lt;br&gt;&lt;font size=&quot;-1&quot;&gt;AN award-winning social business has moved in – both saving and creating jobs.&lt;/font&gt;&lt;br&gt;&lt;font size=&quot;-1&quot; class=&quot;p&quot;&gt;&lt;/font&gt;&lt;br&gt;&lt;font class=&quot;p&quot; size=&quot;-1&quot;&gt;&lt;a class=&quot;p&quot; href=&quot;http://news.google.com/news/more?ncl=d-Kqhhcau4k19TM&amp;amp;authuser=0&amp;amp;ned=uk&quot;&gt;&lt;nobr&gt;&lt;b&gt;and more&amp;nbsp;&amp;raquo;&lt;/b&gt;&lt;/nobr&gt;&lt;/a&gt;&lt;/font&gt;&lt;/div&gt;&lt;/font&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;</description></item><item><title>Bacteria closes Dartmouth&apos;s Birch Cove Beach | The Chronicle Herald - TheChronicleHerald.ca</title><link>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</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>&lt;table border=&quot;0&quot; cellpadding=&quot;2&quot; cellspacing=&quot;7&quot; style=&quot;vertical-align:top;&quot;&gt;&lt;tr&gt;&lt;td width=&quot;80&quot; align=&quot;center&quot; valign=&quot;top&quot;&gt;&lt;font style=&quot;font-size:85%;font-family:arial,sans-serif&quot;&gt;&lt;a href=&quot;http://news.google.com/news/url?sa=t&amp;amp;fd=R&amp;amp;ct2=uk&amp;amp;usg=AFQjCNGMvNI3ZSUFTfUWD6ia0_HcE3gz-A&amp;amp;clid=c3a7d30bb8a4878e06b80cf16b898331&amp;amp;cid=52779548150492&amp;amp;ei=-UtiWbGZHIrkhAGMmrDYBw&amp;amp;url=http://www.metronews.ca/news/halifax/2017/07/05/dartmouth-beach-closed-swimming-high-bacteria.html&quot;&gt;&lt;img src=&quot;//t3.gstatic.com/images?q=tbn:ANd9GcSuM4Lbx1eh7UOr69MB6KAkq5WMI-e3z6loUwqIBzCB_soiJly7oj33gwYTcB8F9QmZvKnZ6EqM&quot; alt=&quot;&quot; border=&quot;1&quot; width=&quot;80&quot; height=&quot;80&quot;&gt;&lt;br&gt;&lt;font size=&quot;-2&quot;&gt;MetroNews Canada&lt;/font&gt;&lt;/a&gt;&lt;/font&gt;&lt;/td&gt;&lt;td valign=&quot;top&quot; class=&quot;j&quot;&gt;&lt;font style=&quot;font-size:85%;font-family:arial,sans-serif&quot;&gt;&lt;br&gt;&lt;div style=&quot;padding-top:0.8em;&quot;&gt;&lt;img alt=&quot;&quot; height=&quot;1&quot; width=&quot;1&quot;&gt;&lt;/div&gt;&lt;div class=&quot;lh&quot;&gt;&lt;a href=&quot;http://news.google.com/news/url?sa=t&amp;amp;fd=R&amp;amp;ct2=uk&amp;amp;usg=AFQjCNHidwNRYJY0vVolosZbesiAkOMpSQ&amp;amp;clid=c3a7d30bb8a4878e06b80cf16b898331&amp;amp;cid=52779548150492&amp;amp;ei=-UtiWbGZHIrkhAGMmrDYBw&amp;amp;url=http://thechronicleherald.ca/metro/1483620-bacteria-closes-dartmouth%25E2%2580%2599s-birch-cove-beach&quot;&gt;&lt;b&gt;Bacteria closes Dartmouth&amp;#39;s Birch Cove Beach | The Chronicle Herald&lt;/b&gt;&lt;/a&gt;&lt;br&gt;&lt;font size=&quot;-1&quot;&gt;&lt;b&gt;&lt;font color=&quot;#6f6f6f&quot;&gt;TheChronicleHerald.ca&lt;/font&gt;&lt;/b&gt;&lt;/font&gt;&lt;br&gt;&lt;font size=&quot;-1&quot;&gt;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&amp;nbsp;...&lt;/font&gt;&lt;br&gt;&lt;font size=&quot;-1&quot;&gt;&lt;a href=&quot;http://news.google.com/news/url?sa=t&amp;amp;fd=R&amp;amp;ct2=uk&amp;amp;usg=AFQjCNGMvNI3ZSUFTfUWD6ia0_HcE3gz-A&amp;amp;clid=c3a7d30bb8a4878e06b80cf16b898331&amp;amp;cid=52779548150492&amp;amp;ei=-UtiWbGZHIrkhAGMmrDYBw&amp;amp;url=http://www.metronews.ca/news/halifax/2017/07/05/dartmouth-beach-closed-swimming-high-bacteria.html&quot;&gt;Dartmouth beach closed to swimming over high bacteria levels ...&lt;/a&gt;&lt;font size=&quot;-1&quot; color=&quot;#6f6f6f&quot;&gt;&lt;nobr&gt;MetroNews Canada&lt;/nobr&gt;&lt;/font&gt;&lt;/font&gt;&lt;br&gt;&lt;font size=&quot;-1&quot; class=&quot;p&quot;&gt;&lt;/font&gt;&lt;br&gt;&lt;font class=&quot;p&quot; size=&quot;-1&quot;&gt;&lt;a class=&quot;p&quot; href=&quot;http://news.google.com/news/more?ncl=dvqw0clrahi5WqMQEdCYeTwyz0taM&amp;amp;authuser=0&amp;amp;ned=uk&quot;&gt;&lt;nobr&gt;&lt;b&gt;all 2 news articles&amp;nbsp;&amp;raquo;&lt;/b&gt;&lt;/nobr&gt;&lt;/a&gt;&lt;/font&gt;&lt;/div&gt;&lt;/font&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;</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
}

但我似乎无法访问每个项目的网址?

有什么建议吗?

1 个答案:

答案 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);
}