Silverlight读取XML出错
类定义如下:
public class NGCatalogItem
{
public string id { get; set; }
public string extend { get; set; }
public NGCatalogItem(){}
public NGCatalogItem(string id, string extend)
{
this.id = id;
this.extend = extend;
}
}
xml文件如下:
<?xml version="1.0" encoding="utf-8" ?>
<dogGis>
<county>
<item>
<id>上当区</id>
<extend> </extend>
</item>
<item>
<id>吴城区</id>
<extend> </extend>
</item>
</county>
</dogGis>
读取xml的代码如下:
//具体读取LeftFoldConfig.xml配置数据,为左侧折叠列表做好准备
void client_OpenWrapPanelReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
if (e.Error != null)
return;
XDocument doc = null;
using (Stream s = e.Result)
{
doc = XDocument.Load(s);
}
List<NGCatalogItem> ngItemList = (from item in doc.Descendants("county")
select new NGCatalogItem
{
id = item.Element("id").Value,
extend = item.Element("id").Value
}).ToList();
}
当程序运行到:
List<NGCatalogItem> ngItemList = (from item in doc.Descendants("county")
select new NGCatalogItem
{
id = item.Element("id").Value,
extend = item.Element("id").Value
}).ToList();
时总是报错:用户代码未处理,NullReferenceException,未将对象引用设置到对象的实例
[解决办法]
错误显示在你的xml返回值中出现空值,试着Debug看看xml读取返回数据是否异常。