Simple XML parsing using LINQ (create an anonymous or real object from XML data)


/ Published in: C#
Save to your folder(s)

See the URL.


Copy this code and paste it in your HTML
  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <Tutorials>
  3. <Tutorial>
  4. <Author>The Reddest</Author>
  5. <Title>
  6. Creating an XP Style WPF Button with Silverlight
  7. </Title>
  8. <Date>2/20/2008</Date>
  9. </Tutorial>
  10. <Tutorial>
  11. <Author>The Fattest</Author>
  12. <Title>
  13. Flex And Yahoo Maps
  14. </Title>
  15. <Date>2/12/2007</Date>
  16. </Tutorial>
  17. <Tutorial>
  18. <Author>The Tallest</Author>
  19. <Title>
  20. WPF Tutorial - Creating A Custom Panel Control
  21. </Title>
  22. <Date>2/18/2008</Date>
  23. </Tutorial>
  24. </Tutorials>
  25.  
  26. XDocument xmlDoc = XDocument.Load("TestFile.xml");
  27.  
  28. var tutorials = from tutorial in xmlDoc.Descendants("Tutorial")
  29. select new
  30. {
  31. Author = tutorial.Element("Author").Value,
  32. Title = tutorial.Element("Title").Value,
  33. Date = tutorial.Element("Date").Value,
  34. };

URL: http://www.switchonthecode.com/tutorials/introduction-to-linq-simple-xml-parsing

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.