Skip to main content

Microsoft Silverlight

Answered Question Not sure why I'm getting a null reference excepting using LINQ TO XML to read a XML file I createdRSS Feed

(0)

byrontech
byrontech

Member

Member

3 points

12 Posts

Not sure why I'm getting a null reference excepting using LINQ TO XML to read a XML file I created

 Hello, everyone

I 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
swildermuth

Star

Star

8320 points

1,546 Posts

Re: Not sure why I'm getting a null reference excepting using LINQ TO XML to read a XML file I created

XAttribute.Value in LINQ to XML is always a string. You need to call DateTime.Parse or TryParse to convert it to a date. 

(If this has answered your question, "Mark as Answer")

Shawn Wildermuth
C# MVP, MCSD, Speaker and Author

Silverlight 3 Workshop
Miami, FL: Oct 12-14th
Portlant, OR: Dec 2-4th
Atlanta, GA: Dec 7-9th
http://silverlight-tour.com

Amanda Wang - MSFT
Amanda W...

All-Star

All-Star

17234 points

1,466 Posts

Answered Question

Re: Not sure why I'm getting a null reference excepting using LINQ TO XML to read a XML file I created

 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")
                        );

Amanda Wang
Microsoft Online Community Support

Please remember to mark the replies as answers if they help and unmark them if they provide no help.

byrontech
byrontech

Member

Member

3 points

12 Posts

Re: Re: Not sure why I'm getting a null reference excepting using LINQ TO XML to read a XML file I created

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>.

  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities