【发布时间】:2014-05-13 19:25:15
【问题描述】:
当我执行代码时,我在 foreach 上遇到了问题。当它第一次执行'foreach'时,它会通过说'对象引用未设置为对象实例'进入'catch'
String u = "C:\\Users\\Lolo\\Desktop\\fluxRSS.xml";
try{
XDocument xDoc = XDocument.Load("myfluxRSS.xml");
var items = (from x in xDoc.Descendants("item")
select new
{
title = x.Element("title").Value,
link = xDoc.Element("link").Value,
pubDate = xDoc.Element("pubDate").Value,
description = xDoc.Element("description").Value
});
if (items != null)
{
foreach (var i in items){
Console.WriteLine(i);
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
RSS 提要是这样的结构:
<xml version="...">
<rss>
<channel>
<item>
<!-- Rest of the tags -->
</item>
<item></item>
<!-- Rest of the items -->
</channel>
<channel>
<item>
<!-- Rest of the tags -->
</item>
<!-- Rest of the items -->
</channel>
</rss>
你有解决办法吗?
【问题讨论】:
标签: c# .net nullreferenceexception