Advertisement
Guest User

04. Import-Rivers-from-XML

a guest
Aug 1st, 2015
375
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.72 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using _01.EF_Mappings;
  7. using System.Xml.Linq;
  8. using System.Data;
  9. using System.Data.SqlClient;
  10. using System.Xml;
  11. using System.Xml.XPath;
  12.  
  13. namespace _04.Import_Rivers_From_XML
  14. {
  15. public class Import_Rivers_From_XML
  16. {
  17. static void Main(string[] args)
  18. {
  19. XmlDocument xmlDoc = new XmlDocument();
  20. xmlDoc.Load("rivers.xml");
  21. string xPath = "/rivers/river";
  22.  
  23. // var riversDB = new River();
  24. XmlNodeList riverList = xmlDoc.SelectNodes(xPath);
  25.  
  26. var contextRivers = new GeographyEntities();
  27. foreach (XmlNode river in riverList)
  28. {
  29. //mandatory
  30. string name = river.FirstChild.InnerText;
  31. int length = Int32.Parse(river["length"].InnerText);
  32. string outflow = river["outflow"].InnerText;
  33. int? drainageArea = null;
  34. int? averageDischarge = null;
  35. string countryRiver = null;
  36.  
  37. //optional
  38.  
  39. if (river["drainage-area"] != null)
  40. {
  41. drainageArea = Int32.Parse(river["drainage-area"].InnerText);
  42.  
  43. }
  44.  
  45.  
  46. if (river["average-discharge"] != null)
  47. {
  48. averageDischarge = Int32.Parse(river["average-discharge"].InnerText);
  49.  
  50. }
  51.  
  52.  
  53. var riversDB = new River()
  54. {
  55. RiverName = name,
  56. Length = length,
  57. Outflow = outflow,
  58. DrainageArea = drainageArea,
  59. AverageDischarge = averageDischarge,
  60.  
  61. };
  62.  
  63. var context = new GeographyEntities();
  64.  
  65. context.Rivers.Add(riversDB);
  66. context.SaveChanges();
  67. Console.WriteLine("Rivers added!");
  68.  
  69. //Error inserting..................................
  70. foreach (XmlNode countryName in river["countries"])
  71. {
  72. countryRiver = countryName.InnerText;
  73. var addCountry = contextRivers.Countries
  74. .FirstOrDefault(c => c.CountryName == countryRiver);
  75. riversDB.Countries.Add(addCountry);
  76.  
  77. context.Rivers.Add(riversDB);
  78. }
  79.  
  80.  
  81. }
  82.  
  83.  
  84.  
  85. var rivers = contextRivers.Rivers
  86. .OrderByDescending(r => r.Length)
  87. .Select(r => new
  88. {
  89. riverName = r.RiverName,
  90. countries = r.Countries
  91. .OrderBy(c => c.CountryName)
  92. .Select(c => new
  93. {
  94. c.CountryName
  95.  
  96. })
  97.  
  98. }).ToList();
  99. foreach (var item in rivers)
  100. {
  101. Console.WriteLine(item.riverName);
  102. foreach (var country in item.countries)
  103. {
  104. Console.WriteLine(country);
  105. }
  106. }
  107.  
  108. }
  109. }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement