Reputation: 676
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?
Upvotes: 0
Views: 220
Reputation: 127
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.
Upvotes: 1