I have this method
public static List<Contact> Load(string filename)
{
if (!File.Exists(filename))
{
throw new FileNotFoundException("Data file could not be found", filename);
}
var contacts =
System.Xml.Linq.XDocument.Load(filename).Root.Elements("Contact").Select
(
x => new Contact() { //errors out here, XXXXXX
FirstName = (string)x.Element("FirstName"),
LastName = (string)x.Element("LastName"),
Email = (string)x.Element("Email")
}
);
return contacts.ToList();// is this line correct?, it should return List...
}
I have Contacts.xml with Contact elements in it.
<Contacts>
<Contact>
<FirstName>Mike</FirstName>
<LastName>Phipps</LastName>
<Email>[email protected]</Email>
</Contact>
<Contact>
<FirstName>Holly</FirstName>
<LastName>Holt</LastName>
<Email>[email protected]</Email>
</Contact>
<Contact>
<FirstName>Liz</FirstName>
<LastName>Keyser</LastName>
</Contact>
</Contacts>
I have a contact.cs with this code
public class Contact
{
public Contact(string firstName, string lastName, string email)
{
FirstName = firstName;
LastName = lastName;
Email = email;
}
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public string PhoneNumber { get; set; }
public string Address { get; set; }
}
on the line, where stamped with 'XXXXXX', how should i change the line to make it work?