好的。所以,与Windows Phone 7的应用程序,说我有下面的XML文件从XML文档加载数据在Windows Phone上使用XDocument时引发NullReferenceException 7
<Objects>
<Object Property1=”Value1” Property2=”Value2”>
<Property3>Value3</Property3>
</Object>
<Object Property1=”Value1” Property2=”Value2”>
<Property3>Value3</Property3>
</Object>
</Objects>
而且我有下面的类定义
public class myObject
{
public string Property1 { get; set; }
public string Property2 { get; set; }
public string Property3 { get; set; }
public myObject (string _property1, string _property2, string _property3)
{
this.Property1 = _property1
this.Property1 = _property1
this.Property1 = _property1
}
}
,然后我用下面的代码从XML加载数据文件并返回一个myObjects列表: -
var xdoc = XDocument.Load("myXMLFile.xml");
var result = from o in xdoc.Document.Descendants("Object")
select new myObject
{
Property1 = o.Element("Property1").Value,
Property2 = o.Element("Property2").Value,
Property3 = o.Element("Property3").Value,
};
return result.ToList<myObject>();
为什么这会返回一个NullReferenceException?我猜这是我的linq查询不是很正确,因为该文件正在加载XDocument.Load调用罚款。
任何帮助将是太棒了!
克里斯
+1属性1,属性2是看你的xml。在我可以回复之前分神,这应该排除你的问题。 – 2010-12-17 01:57:00