J'ai cette méthodeC#, expressions lambda, où est l'erreur?
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...
}
Je Contacts.xml avec des éléments de contact en elle.
<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>
J'ai contact.cs avec ce 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; }
}
sur la ligne où estampillé « xxxxxx », comment dois-je changer la ligne pour le faire fonctionner?
merci, le code compile maintenant. – user149169