code_junkie

LINQ to XML, ORM or someting “Completely Different”

Nov 13th, 2011
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.13 KB | None | 0 0
  1. <CODEBOOKINDEX>
  2. <ME Words="1" Score="25" Highscore="1">Main Entry Item
  3. <NM>attack</NM>
  4. <NM>cardiac</NM>
  5. <NM>chest</NM>
  6. <NM>effort</NM>
  7. <NM>heart</NM>
  8. <NM>pectoris</NM>
  9. <NM>syndrome</NM>
  10. <NM>vasomotor</NM>
  11. <IE>413.9</IE>
  12.  
  13. <M1 Words="1" Score="25">An M1 Item (Same as ME, just first level Child)
  14. <IE>557.1</IE>
  15. </M1>
  16.  
  17. <M1 Words="1" Score="25">Another M1 Item
  18. <IE>443.9</IE>
  19. <M2 Words="1" Score="25">An M2 Item (again same as ME, just a child of an M1 item)
  20. <CF>Arteriosclerosis,extremities</CF>
  21. <IE>440.20</IE>
  22. </M2>
  23. </M1>
  24. </ME></CODEBOOKINDEX>
  25.  
  26. public CustomClass TranslateME(XElement source)
  27. {
  28. CustomClass result = new CustomClass();
  29. result.Words = (int) source.Attribute("Words");
  30. result.Score = (int) source.Attribute("Score");
  31.  
  32. XAttribute highScore = source.Attribute("HighScore");
  33. result.HighScore = (highScore == null) ? 0 : (int) highScore;
  34.  
  35. result.NMs = source
  36. .Elements("NM")
  37. .Select(x => x.Value)
  38. .ToList();
  39.  
  40. result.IE = source
  41. .Element("IE").Value;
  42.  
  43. result.SubEntries = source
  44. .Elements("M1")
  45. .Select(x => TranslateM1(x))
  46. .ToList();
  47.  
  48. return result;
  49. }
Add Comment
Please, Sign In to add comment