0

I have a XML file names Customerx.xml. It has the parent tag as . It has many .

My customer class is defined as:

public class Customer
{
  private List<Customer> customerList;

  public string CustomerID { get; set; }
  public string CompanyName { get; set; }
  public string Address { get; set; }
  public string City { get; set; }
  public string Region { get; set; }
  public string PostalCode { get; set; }
  public string Country { get; set; }
  public string Phone { get; set; }
  public string Fax { get; set; }

  public List<Customer> GetCustomerList()
  {
     if (customerList == null)
        CreateCustomerLists();

     return customerList;
  }

  private void CreateCustomerLists()
  {
     var list = from e in XDocument.Load("Customers.xml").Root.Elements("customer")
                 select new Customer
                            {
                                CustomerID = (string) e.Element("id"),
                                CompanyName = (string) e.Element("name"),
                                Address = (string) e.Element("address"),
                                City = (string) e.Element("city"),
                                Region = (string) e.Element("region"),
                                PostalCode = (string) e.Element("postalcode"),
                                Country = (string) e.Element("country"),
                                Phone = (string) e.Element("phone"),
                                Fax = (string) e.Element("fax"),
                            };

      customerList = list.ToList();
  }
}

The data in the XML is:

<?xml version="1.0"?>
<customers>
  <customer>
    <id>ALFKI</id>
    <name>Alfreds Futterkiste</name>
    <address>Obere Str. 57</address>
    <city>Berlin</city>
    <postalcode>12209</postalcode>
    <country>Germany</country>
    <phone>030-0074321</phone>
    <fax>030-0076545</fax>
  </customer>
  <customer>
    <id>ANATR</id>
    <name>Ana Trujillo Emparedados y helados</name>
    <address>Avda. de la Constitución 2222</address>
    <city>México D.F.</city>
    <postalcode>05021</postalcode>
    <country>Mexico</country>
    <phone>(5) 555-4729</phone>
    <fax>(5) 555-3745</fax>
  </customer>

In the CreateCustomerLists() method I am getting a null reference execption while reading the data from xlm file? Why is this happening? How can I get rid of it?

afuzzyllama
  • 6,538
  • 5
  • 47
  • 64
Pankouri
  • 676
  • 3
  • 12
  • 29
  • 2
    Which line? What does your XML look like? A short but *complete* program demonstrating the problem would really help... – Jon Skeet May 01 '13 at 15:12
  • Almost all cases of `NullReferenceException` are the same. Please see "[What is a NullReferenceException in .NET?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net)" for some hints. – John Saunders May 01 '13 at 15:19
  • @JonSkeet, I have edited my question. now I have provided two xml data as well. the line I am getting error on var list = from ... this line on CreateCustomerLists() mehod. – Pankouri May 01 '13 at 15:20
  • Did not find region in your xml. This might be the culprit for null reference – ZafarYousafi May 01 '13 at 15:34
  • Can you give as the exact exception with stacktrace? – Martin Mulder May 01 '13 at 15:38

1 Answers1

1

To answer your other question: if you're running from Debug in Visual Studio then the debug folder will be the start of the relative path it uses to look for files. Any files you need to include should be placed in there.

Kyle Carruthers
  • 129
  • 1
  • 8