I converted this from a C# project that I found online so I am not 100% sure that I did it right but it looks right to me. Here is the code I have:
VB Code:
Public Sub InitializeGrid() Dim dg As DataGrid = TryCast(FindName("datagrid1"), DataGrid) Dim doc As XDocument = XDocument.Load("StoresList2.xml") Dim StoreData = From info In doc.Descendants("Store") _ Select New Stores With { _ .Name = Convert.ToString(info.Element("Name").Value), _ .Number = Convert.ToString(info.Element("Number").Value), _ .Phone = Convert.ToString(info.Element("Phone").Value)} dg.ItemsSource = StoreData End Sub
My Class looks like this:
VB Code:
Public Class Stores Private Property _Number As String Public Property Number() As String Get Return _Number End Get Set(ByVal value As String) _Number = value End Set End Property Private _Name As String Public Property Name() As String Get Return _Name End Get Set(ByVal value As String) _Name = value End Set End Property Private _Phone As String Public Property Phone As String Get Return _Phone End Get Set(ByVal value As String) _Phone = value End Set End Property End Class
And then my XML file looks like this:
XML Code:
<?xml version="1.0" encoding="utf-8" ?> <company> <store> <Number>100</Number> <Name>Cho Cho Puff Inc</Name> <Phone>269-555-1212</Phone> </store> <store> <Number>101</Number> <Name>Cheerios Inc</Name> <Phone>616-555-5555</Phone> </store> </company>
The error that I am getting is this:
System.NullReferenceException was unhandled by user code
Message=Object reference not set to an instance of an object.
on this line dg.ItemsSource = StoreData in the InitializeGrid sub
I have stepped through the code and the data is being read from the file so I am not sure what the problem is. Any ideas?




Reply With Quote