0

Hi there I'm trying to parse an XML file into a list of contacts, but having problems:

    public List<ContactModel> GetContacts()
    {

        var doc = XDocument.Load(HttpContext.Current
                                            .Server
                                            .MapPath(@"..\App_Data\Contacts.xml"));

        var result = from items in doc.Descendants("Directory")
                     select new ContactModel()
                                {
                                    Id = items.Attribute("ID").Value,
                                    FirstName = items.Attribute("FirstName").Value,
                                    LastName = items.Attribute("LastName").Value,
                                    Telephone = items.Attribute("Telephone").Value,
                                    Email = items.Attribute("Email").Value,
                                    Room = items.Attribute("Room").Value,
                                    Building = items.Attribute("Building").Value,
                                    Location = items.Attribute("Location").Value
                                };

        List<ContactModel> contactList = new List<ContactModel>();
        foreach (var item in result)
        {
            contactList.Add(item);
        }

        return contactList;
    }

I get a null exception when it's trying to loop, what am I doing wrong?

This is my XML

<?xml version="1.0" standalone="yes"?>
<ContactDirectory>
  <Directory>
    <ID>1</ID>
    <FirstName>Peter</FirstName>
    <LastName>Sutt</LastName>
    <Telephone>777888</Telephone>
    <Email>[email protected]</Email>
    <Room>3.44</Room>
    <Building>Westside</Building>
    <Location>Leeds</Location>
  </Directory>
  <Directory>
    <ID>2</ID>
    <FirstName>Fred</FirstName>
    <LastName>West</LastName>
    <Telephone>1234</Telephone>
    <Email>[email protected]</Email>
    <Room>1.23</Room>
    <Building>Cromwell St</Building>
    <Location>Gloster</Location>
  </Directory>
  <Directory>
</ContactDirectory>
1
  • Error is {"Object reference not set to an instance of an object."} on the Select new ContactModel Commented Oct 2, 2012 at 11:34

3 Answers 3

3

Beebul, They are Elements not Attributes

var contactList = (from items in doc.Descendants("Directory")
                select new ContactModel()
                {
                    Id = items.Element("ID").Value,
                    FirstName = items.Element("FirstName").Value,
                    LastName = items.Element("LastName").Value,
                    Telephone = items.Element("Telephone").Value,
                    Email = items.Element("Email").Value,
                    Room = items.Element("Room").Value,
                    Building = items.Element("Building").Value,
                    Location = items.Element("Location").Value
                })
                .ToList();

PS: you don't need to loop over your result to get a list. You can use ToList()

Sign up to request clarification or add additional context in comments.

Comments

2

It looks like one or more of your attributes are missing. items.Attribute(...) returns null, and calling Value on it causes the NPE*.

Since the execution is deferred, the call does not happen until you start looping through the result.

To find the attribute that causes the problem remove all calls to Attribute(...) except for ID, verify that the crash does not happen, and start adding back the attributes one by one until the crash is back.


* After seeing the XML that you added to your question it appears that all attributes are missing! Here is a link to a short article that discusses the differences.

Comments

0

The Linq to XML API has implicit conversions to help with possible null references returned from .Value. Try something like (string) items.Attribute("Room").

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.