LINQ to XML

This simple example downloads an XML document from the Internet, parses it using LINQ, and displays its data.

using System;
using System.Linq;
using System.Xml.Linq;

namespace LinqExample
{
	class Program
	{
		static void Main()
		{
			// download XML document from the Internet
			XDocument xdoc = XDocument.Load("http://www.prowaretech.com/_xml.aspx");

			var query = from x in xdoc.Descendants("author") // not "authors"
						select new
						{
							SSN = x.Attribute("ssn").Value,
							LastName = x.Element("lastname").Value,
							FirstName = x.Element("firstname").Value,
							Phone = x.Element("phone").Value
						};

			foreach (var record in query)
			{
				Console.WriteLine(record.SSN + ": " +
									record.LastName + ", " +
									record.FirstName + " - " +
									record.Phone);
			}
		}
	}
}
172-32-1176: White, Johnson - 408 496-7223
213-46-8915: Green, Marjorie - 415 986-7020
238-95-7766: Carson, Cheryl - 415 548-7723
267-41-2394: O'Leary, Michael - 408 286-2428
274-80-9391: Straight, Dean - 415 834-2919
341-22-1782: Smith, Meander - 913 843-0462
409-56-7008: Bennet, Abraham - 415 658-9932
427-17-2319: Dull, Ann - 415 836-7128
472-27-2349: Gringlesby, Burt - 707 938-6445
486-29-1786: Locksley, Charlene - 415 585-4620
527-72-3246: Greene, Morningstar - 615 297-2723
648-92-1872: Blotchet-Halls, Reginald - 503 745-6402
672-71-3249: Yokomoto, Akiko - 415 935-4228
712-45-1867: del Castillo, Innes - 615 996-8275
722-51-5454: DeFrance, Michel - 219 547-9982
724-08-9931: Stringer, Dirk - 415 843-2991
724-80-9391: MacFeather, Stearns - 415 354-7128
756-30-7391: Karsen, Livia - 415 534-9219
807-91-6654: Panteley, Sylvia - 301 946-8853
846-92-7186: Hunter, Sheryl - 415 836-7128
893-72-1158: McBadden, Heather - 707 448-4982
899-46-2035: Ringer, Anne - 801 826-0752
998-72-3567: Ringer, Albert - 801 826-0752

The XML source:

<?xml version="1.0" encoding="utf-8"?>
<authors>
	<author
	ssn="172-32-1176">
	<phone>408 496-7223</phone>
	<lastname>White</lastname>
	<firstname>Johnson</firstname>
	</author>
	<author
	ssn="213-46-8915">
	<phone>415 986-7020</phone>
	<lastname>Green</lastname>
	<firstname>Marjorie</firstname>
	</author>
	<author
	ssn="238-95-7766">
	<phone>415 548-7723</phone>
	<lastname>Carson</lastname>
	<firstname>Cheryl</firstname>
	</author>
	<author
	ssn="267-41-2394">
	<phone>408 286-2428</phone>
	<lastname>O'Leary</lastname>
	<firstname>Michael</firstname>
	</author>
	<author
	ssn="274-80-9391">
	<phone>415 834-2919</phone>
	<lastname>Straight</lastname>
	<firstname>Dean</firstname>
	</author>
	<author
	ssn="341-22-1782">
	<phone>913 843-0462</phone>
	<lastname>Smith</lastname>
	<firstname>Meander</firstname>
	</author>
	<author
	ssn="409-56-7008">
	<phone>415 658-9932</phone>
	<lastname>Bennet</lastname>
	<firstname>Abraham</firstname>
	</author>
	<author
	ssn="427-17-2319">
	<phone>415 836-7128</phone>
	<lastname>Dull</lastname>
	<firstname>Ann</firstname>
	</author>
	<author
	ssn="472-27-2349">
	<phone>707 938-6445</phone>
	<lastname>Gringlesby</lastname>
	<firstname>Burt</firstname>
	</author>
	<author
	ssn="486-29-1786">
	<phone>415 585-4620</phone>
	<lastname>Locksley</lastname>
	<firstname>Charlene</firstname>
	</author>
	<author
	ssn="527-72-3246">
	<phone>615 297-2723</phone>
	<lastname>Greene</lastname>
	<firstname>Morningstar</firstname>
	</author>
	<author
	ssn="648-92-1872">
	<phone>503 745-6402</phone>
	<lastname>Blotchet-Halls</lastname>
	<firstname>Reginald</firstname>
	</author>
	<author
	ssn="672-71-3249">
	<phone>415 935-4228</phone>
	<lastname>Yokomoto</lastname>
	<firstname>Akiko</firstname>
	</author>
	<author
	ssn="712-45-1867">
	<phone>615 996-8275</phone>
	<lastname>del Castillo</lastname>
	<firstname>Innes</firstname>
	</author>
	<author
	ssn="722-51-5454">
	<phone>219 547-9982</phone>
	<lastname>DeFrance</lastname>
	<firstname>Michel</firstname>
	</author>
	<author
	ssn="724-08-9931">
	<phone>415 843-2991</phone>
	<lastname>Stringer</lastname>
	<firstname>Dirk</firstname>
	</author>
	<author
	ssn="724-80-9391">
	<phone>415 354-7128</phone>
	<lastname>MacFeather</lastname>
	<firstname>Stearns</firstname>
	</author>
	<author
	ssn="756-30-7391">
	<phone>415 534-9219</phone>
	<lastname>Karsen</lastname>
	<firstname>Livia</firstname>
	</author>
	<author
	ssn="807-91-6654">
	<phone>301 946-8853</phone>
	<lastname>Panteley</lastname>
	<firstname>Sylvia</firstname>
	</author>
	<author
	ssn="846-92-7186">
	<phone>415 836-7128</phone>
	<lastname>Hunter</lastname>
	<firstname>Sheryl</firstname>
	</author>
	<author
	ssn="893-72-1158">
	<phone>707 448-4982</phone>
	<lastname>McBadden</lastname>
	<firstname>Heather</firstname>
	</author>
	<author
	ssn="899-46-2035">
	<phone>801 826-0752</phone>
	<lastname>Ringer</lastname>
	<firstname>Anne</firstname>
	</author>
	<author
	ssn="998-72-3567">
	<phone>801 826-0752</phone>
	<lastname>Ringer</lastname>
	<firstname>Albert</firstname>
	</author>
</authors>
<< < [Page 7 of 7] > >>