It's just a matter of reading the value of the element name. Here's a sample code based on the article. Notice that I have commented out the Fax element since there are a few records with no Fax element. Therefore, if you need to iterate thru your records in the XML document, you need to implement your own error handling mechanism. Paste the code inside the static void Main(string[ ] args)
// Create the query
var custs = from c in XElement.Load("Customers.xml").Elements("Customers")
select new
{
CustomerID = c.Element("CustomerID").Value,
CompanyName = c.Element("CompanyName").Value,
ContactName = c.Element("ContactName").Value,
ContactTitle = c.Element("ContactTitle").Value,
Address = c.Element("Address").Value,
City = c.Element("City").Value,
PostalCode = c.Element("PostalCode").Value,
Country = c.Element("Country").Value,
Phone = c.Element("Phone").Value,
//Fax = c.Element("Fax").Value
};
// Execute the query
foreach (var customer in custs)
{
Console.WriteLine("CustomerID:" + customer.CustomerID);
Console.WriteLine("Company Name:" + customer.CompanyName);
Console.WriteLine("Contact Name:" + customer.ContactName);
Console.WriteLine("Contact Title:" + customer.ContactTitle);
Console.WriteLine("Address:" + customer.Address);
Console.WriteLine("City:" + customer.City);
Console.WriteLine("Postal Code:" + customer.PostalCode);
Console.WriteLine("Country:" + customer.Country);
Console.WriteLine("Phone:" + customer.Phone);
//Console.WriteLine("Fax:" + customer.Fax);
Console.WriteLine("========================");
}
//Pause the application
Console.ReadLine();