Created
July 2, 2014 13:47
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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