Skip to main content
Home Forums Silverlight Programming Programming with .NET - General Not sure why I'm getting a null reference excepting using LINQ TO XML to read a XML file I created
3 replies. Latest Post by byrontech on January 14, 2009.
(0)
byrontech
Member
3 points
12 Posts
01-11-2009 8:01 PM |
Hello, everyoneI been messing around with LINQ to XML to read XML data and I created a project with Silverlight 2.0 that uses LINQ to XML to read a XML file I created. However, when my code hits the select block of the query I get a null reference exception and VS2008 highlights the entire block
Here is my XML:
<?xml version="1.0" encoding="utf-8" ?><blogs> <item> <PostDate data="1/10/2009"/> <Author data="Byron Blank"/> <Content data="Today, I have implemented Microsoft Silverlight 2.0 Support on Byron Tech! This very news posting is done in SilverLight! I also have a test page where I have created Silverlight animated buttons for this sites navigation at: http://byron-tech.gotdns.com/testpage.html. For more information about Silverlight, please visit http://www.silverlight.net."/> </item> </blogs>
Here is my Page.xml.cs:
using System;using System.Windows;using System.Windows.Controls;using System.Windows.Documents;using System.Windows.Ink;using System.Windows.Input;using System.Windows.Media;using System.Windows.Media.Animation;using System.Windows.Shapes;using System.Xml;using System.Xml.Linq;using System.Linq;using System.Collections;using System.Collections.Generic;namespace SiteNews{ public partial class Page : UserControl { public Page() { // Required to initialize variables InitializeComponent(); this.Loaded += new RoutedEventHandler(Page_Loaded); } void Page_Loaded(object sender, RoutedEventArgs e) { XElement myBlogs = XElement.Load(XmlReader.Create("blogs.xml")); IEnumerable<Blogs> blogList = from blog in myBlogs.Descendants("item") select new Blogs ( (DateTime)(myBlogs.Element("PostDate").Attribute("data")), (string)myBlogs.Element("Author").Attribute("data"), (string)myBlogs.Element("Content").Attribute("data") ); TestData.ItemsSource = blogList; } }} Here is my Blogs class:
using System;using System.Net;using System.Windows;using System.Windows.Controls;using System.Windows.Documents;using System.Windows.Ink;using System.Windows.Input;using System.Windows.Media;using System.Windows.Media.Animation;using System.Windows.Shapes;namespace SiteNews{ public class Blogs { public DateTime PostDate { get; set; } public String Author { get; set; } public String Content { get; set; } public Blogs(DateTime postdate, string author, string content) { this.PostDate = postdate; this.Author = author; this.Content = content; } } }If you need more information feel free to ask for it in the thread.
swildermuth
Star
8320 points
1,546 Posts
01-11-2009 9:36 PM |
XAttribute.Value in LINQ to XML is always a string. You need to call DateTime.Parse or TryParse to convert it to a date.
Amanda W...
All-Star
17234 points
1,466 Posts
01-14-2009 9:54 PM |
Hi,
byrontech:void Page_Loaded(object sender, RoutedEventArgs e) { XElement myBlogs = XElement.Load(XmlReader.Create("blogs.xml")); IEnumerable<Blogs> blogList = from blog in myBlogs.Descendants("item") select new Blogs ( (DateTime)(myBlogs.Element("PostDate").Attribute("data")), (string)myBlogs.Element("Author").Attribute("data"), (string)myBlogs.Element("Content").Attribute("data") ); TestData.ItemsSource = blogList; }
Try to change your above code black like below, use blog instead of myBlogs in the select section, it works fine on our labs:
IEnumerable<Blogs> blogList = from blog in myBlogs.Descendants("item") select new Blogs ( (DateTime)(blog .Element("PostDate").Attribute("data")), (string)blog .Element("Author").Attribute("data"), (string)blog .Element("Content").Attribute("data") );
01-14-2009 10:41 PM |
Thanks this worked out perfectly. And I know why. Since myBlogs contained the entire XML document (I noticed this while debugging) and not just the decendants of <item>. blog however did contain the XML of the decendants of <item>.