Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8614851
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T05:13:21+00:00 2026-06-12T05:13:21+00:00

Hi there I’m trying to parse an XML file into a list of contacts,

  • 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>pett@gmail.com</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>fred@west.com</Email>
    <Room>1.23</Room>
    <Building>Cromwell St</Building>
    <Location>Gloster</Location>
  </Directory>
  <Directory>
</ContactDirectory>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T05:13:22+00:00Added an answer on June 12, 2026 at 5:13 am

    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()

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There is a list standard python exceptions that we should watch out, but I
There's a lot of reading on self referencing problems, but I can't seem to
There's a Rails 3.2.3 web application which doesn't use any database. But in spite
There is a directed graph having a single designated node called root from which
There are nice SO question and answers about this issue, but these options didn't
There was a smbmrx sample code using RDBSS in WDK Vista. But since WDK
I am trying to render a haml file in a javascript response like so:
There was a thread on this in comp.lang.javascript recently where victory was announced but
there is a web site called: SPAM I am trying to make the same
There is a trigger on a table for updates, inserts, and deletes, but the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.