Пытаюсь прочитать xml файл, во вложениях тэга entry, значение тэгов link, updated, title, НО не получается, выходит ошибка "System.NullReferenceException: "Ссылка на объект не указывает на экземпляр объекта." n было null."
XmlReader xmlreader = XmlReader.Create(@"D:\RssFeed\www.redditc.xml");
while (xmlreader.Read())
{
if (xmlreader.NodeType == XmlNodeType.Element)
{
string[] xmlNode = {"entry"};
string[] selectNode = {"link", "updated", "title" };
XmlDocument xmlDoc = new XmlDocument();
foreach (var node in xmlNode)
{
if (xmlreader.Name == node)
{
xmlDoc.LoadXml(xmlreader.ReadOuterXml());
XmlNode n = xmlDoc.SelectSingleNode(node);
foreach (string item in selectNode)
{
string selNode = n.SelectSingleNode(item).InnerText;
Console.WriteLine(selNode);
}
}
}
}
}
Вот сам файл xml
<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<category term="worldnews" label="r/worldnews" />
<updated>2019-08-07T09:34:39+00:00</updated>
<icon>https://www.redditstatic.com/icon.png/</icon>
<id>/r/worldnews/.rss</id>
<link rel="self" href="https://www.reddit.com/r/worldnews/.rss" type="application/atom+xml" />
<link rel="alternate" href="https://www.reddit.com/r/worldnews/" type="text/html" />
<subtitle>A place for major news from around the world, excluding US-internal news.</subtitle>
<title>World News</title>
<entry>
<author>
<name>/u/vannybros</name>
<uri>https://www.reddit.com/user/vannybros</uri>
</author>
<category term="worldnews" label="r/worldnews" />
<content type="html">&#32; submitted by &#32; <a href="https://www.reddit.com/user/vannybros"> /u/vannybros </a> <br/> <span><a href="https://www.taiwannews.com.tw/en/news/3759806">[link]</a></span> &#32; <span><a href="https://www.reddit.com/r/worldnews/comments/cmxnwn/hong_kong_police_tear_skirt_underwear_off_female/">[comments]</a></span></content>
<id>t3_cmxnwn</id>
<link href="https://www.reddit.com/r/worldnews/comments/cmxnwn/hong_kong_police_tear_skirt_underwear_off_female/" />
<updated>2019-08-06T23:06:01+00:00</updated>
<title>Hong Kong police tear skirt, underwear off female protester, sparking public wrath</title>
</entry>
<entry>
<author>
<name>/u/Frocharocha</name>
<uri>https://www.reddit.com/user/Frocharocha</uri>
</author>
<category term="worldnews" label="r/worldnews" />
<content type="html">&#32; submitted by &#32; <a href="https://www.reddit.com/user/Frocharocha"> /u/Frocharocha </a> <br/> <span><a href="https://www.usatoday.com/story/travel/news/2019/08/06/mass-shootings-force-foreign-countries-issue-us-travel-warnings/1930215001/">[link]</a></span> &#32; <span><a href="https://www.reddit.com/r/worldnews/comments/cmvpdx/venezuela_uruguay_japan_warn_citizens_about/">[comments]</a></span></content>
<id>t3_cmvpdx</id>
<link href="https://www.reddit.com/r/worldnews/comments/cmvpdx/venezuela_uruguay_japan_warn_citizens_about/" />
<updated>2019-08-06T20:23:43+00:00</updated>
<title>Venezuela, Uruguay, Japan warn citizens about traveling to the US because of mass shootings</title>
</entry>
<entry>
<author>
<name>/u/PreMedSomewhere</name>
<uri>https://www.reddit.com/user/PreMedSomewhere</uri>
</author>
<category term="worldnews" label="r/worldnews" />
<content type="html">&#32; submitted by &#32; <a href="https://www.reddit.com/user/PreMedSomewhere"> /u/PreMedSomewhere </a> <br/> <span><a href="https://www.cnn.com/2019/08/06/business/dominos-stockpiling-ingredients-gbr-intl-scli/index.html">[link]</a></span> &#32; <span><a href="https://www.reddit.com/r/worldnews/comments/cmt9c1/dominos_is_stockpiling_pizza_ingredients_to/">[comments]</a></span></content>
<id>t3_cmt9c1</id>
<link href="https://www.reddit.com/r/worldnews/comments/cmt9c1/dominos_is_stockpiling_pizza_ingredients_to/" />
<updated>2019-08-06T17:04:26+00:00</updated>
<title>Domino's is stockpiling pizza ingredients to protect against a disorderly Brexit</title>
</entry>
</feed>