I am doing a prototype with a Grid. I am reading an XML file to populate the grid. I am getting an unhandled exception that I cannot understand.
Line: 61Error: Unhandled Error in Silverlight Application Code: 4004 Category: ManagedRuntimeError Message: System.NullReferenceException: Object reference not set to an instance of an object. at NxServer.Views.PatientDetail.client_DownloadModuleDataCompleted(Object sender, DownloadStringCompletedEventArgs e) at System.Net.WebClient.OnDownloadStringCompleted(DownloadStringCompletedEventArgs e) at System.Net.WebClient.DownloadStringOperationCompleted(Object arg)
Here is the code.
<igGrid:XamWebGrid x:Name="gridDiseaseModules" AutoGenerateColumns="True"/>
In the code behind.
void client_DownloadModuleDataCompleted(object sender, DownloadStringCompletedEventArgs e){ using (TextReader XmlData = new StringReader(e.Result)) { //<modules> // <module> // <path_id>1test</path_id> // <version>2009-07-29 17:45:49</version> // <path_desc>1test</path_desc> // <status>N/A</status> // </module> //</modules> XDocument XmlModules = XDocument.Load(XmlData); var Modules = from ModuleNode in XmlModules.Descendants("module") select new Module { PathId = ModuleNode.Element("path_id").Value, Version = Convert.ToDateTime(ModuleNode.Element("version").Value), Description = ModuleNode.Element("path_desc").Value, Status = ModuleNode.Element("status").Value }; gridDiseaseModules.ItemsSource = Modules.ToList(); }}
I can iterate through the Modules collection, so I know the data is being read correctly.
It has to be something simple.
From the stack trace, it does not look like the exception is coming from the grid. Are you sure that every element in your XML file has a value? When LINQ executes the query, if it tries to create a Module object and the XML node is empty I beleive it throws a null ref exception.
Devin
Here is my complete XML file
<?xml version="1.0"?><modules> <module> <path_id>1test</path_id> <version>2009-07-29 17:45:49</version> <path_desc>1test</path_desc> <status>N/A</status> </module></modules>
Hey Jim,
I tested out your code, and it worked fine.
The only thing i did differently was load the xml file directly instead of downloading it.
public class Module
{
public string PathId
get;
set;
}
public DateTime Version
public string Description
public string Status
XDocument doc = XDocument.Load("blegh.xml");
var Modules = from ModuleNode in doc.Descendants("module")
select new Module
PathId = ModuleNode.Element("path_id").Value,
Version = Convert.ToDateTime(ModuleNode.Element("version").Value),
Description = ModuleNode.Element("path_desc").Value,
Status = ModuleNode.Element("status").Value
};
this.DataGrid1.ItemsSource = Modules.ToList();
-SteveZ