We help IT Professionals succeed at work.

Cannot access certain field in the XML file using XML to LINQ

3xp0
3xp0 asked
on
417 Views
Last Modified: 2008-06-04
I'm receiving a xml feed containing car adverts from another site.
And when I try to read this using XML to LINQ I get 'Object reference not set to an instance of an object.
on certain elements.
I can get 8 out of 10 elements, but then I get  'Object reference not set to an instance of an object.' on the rest.
The elements I'm having problems with are commented out in the code attached.

URL to complete result from feed: http://dev2.spire.as/feed.aspx
string feedURL = @"http://www.url-to-feed.com";
        XDocument rssFeed = XDocument.Load(feedURL);
        var posts = from a in rssFeed.Descendants("element")
                    select new Article
                    {
                        //Image = a.Element("MMO").Value,
                        Price = int.Parse(a.Element("PRICE.AMOUNT").Value),
                        ModelSpecification = a.Element("CAR_MODEL.MODEL_SPECIFICATION").Value,
                        //Disposed = int.Parse(a.Element("DISPOSED").Value),
                        Make = a.Element("CAR_MODEL.MAKE").Value,
                        Mileage = int.Parse(a.Element("MILEAGE").Value),
                        Heading = a.Element("HEADING").Value,
                        ArticleID = int.Parse(a.Element("ADID").Value),
                        Year = int.Parse(a.Element("YEAR_MODEL").Value),
                        Location = a.Element("LOCATION").Value,
                    };

Open in new window

Comment
Watch Question

Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Commented:
The top solution didn't work for me, but I found this one online that did.
var result = from s in data.Descendants("student")
//THE SNIPPET
             let user = s.Attribute("user")
            where user != null
//THE SNIPPET
             select new LINQandXML.Student
             {
                 Family = s.Element("family").Value,
                 Given = s.Element("given").Value,
                 User = s.Element("user").Value
             };

Open in new window

Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.