Skip to content

Instantly share code, notes, and snippets.

@kdarty
Created July 2, 2014 13:47
Show Gist options
  • Save kdarty/f2f4189bf20565eaa280 to your computer and use it in GitHub Desktop.
Save kdarty/f2f4189bf20565eaa280 to your computer and use it in GitHub Desktop.
Simple Example of using LINQ 2 XML to Parse XML Data Entries into C# Objects
// Simple Example of using LINQ 2 XML to Parse XML Data Entries into C# Objects
// Author: Sergey Berezovskiy
// Source: http://stackoverflow.com/questions/14226473/c-sharp-parsing-xml-file
// NOTE: Your XML file should have a single root node
var xdoc = XDocument.Load(path_to_xml);
var entries = from e in xdoc.Descendants("entry")
select new {
Id = (int)e.Attribute("id"),
Type = (string)e.Attribute("type"),
Name = (string)e.Element("name"),
Description = (string)e.Element("description")
};
// The above Query will return sequence of anonymous objects corresponding to each
// entry element (with properties Id, Type, Name, and Description).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment