Skip to content

Instantly share code, notes, and snippets.

@okaram
Last active August 29, 2015 13:58
Show Gist options
  • Save okaram/9947768 to your computer and use it in GitHub Desktop.
Save okaram/9947768 to your computer and use it in GitHub Desktop.
Super Quick Linq to XML
<contacts>
<contact id="1">
<firstName>Orlando</firstName>
<lastName>Karam</lastName>
</contact>
<contact id="2">
<firstName>Lina</firstName>
<lastName>Colli</lastName>
</contact>
</contacts>
XDocument loaded = XDocument.Load(@"C:\contacts.xml");
var contacts = from c in loaded.Descendants("contact")
select new {
id=(int)c.Attribute("id"),
name=(string)c.Element("firstName") +" " +(string)c.Element("lastName"),
};
foreach (var c in contacts)
Console.WriteLine("Contact: id={0}, name = {1}", c.id, c.name);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment