Как создать DataTable каждый раз когда меняется значение атрибута в xml файле или как мне посчитать количество - C#

Узнай цену своей работы

Формулировка задачи:

Вот пример для одного DataTable, а мне надо сделать List<DataTable> - ов, когда меняется значение атрибута path. А пока у меня все точки point записывает в одну таблицу DataTable.
Листинг программы
  1. private DataTable TablePoint()
  2. {
  3. //создаём таблицу
  4. DataTable dtPoint = new DataTable();
  5. //создаём три колонки
  6. DataColumn x = new DataColumn("x", typeof(Double));
  7. DataColumn y = new DataColumn("y", typeof(Double));
  8. //добавляем колонки в таблицу
  9. dtPoint.Columns.Add(x);
  10. dtPoint.Columns.Add(y);
  11. return dtPoint;
  12. }
  13. listDt.Add(TablePoint());
  14. DataRow newRowListDt = null;
  15. foreach (XElement elm in xDoc.Descendants("point"))
  16. {
  17. newRowListDt = listDt[0].NewRow();
  18. if (elm.Element("x") != null)
  19. {
  20. newRowListDt["x"] = double.Parse(elm.Element("x").Value);
  21. }
  22. if (elm.Element("y") != null)
  23. {
  24. newRowListDt["y"] = double.Parse(elm.Element("y").Value);
  25. }
  26. listDt[0].Rows.Add(newRowListDt);
  27. }
Листинг программы
  1. <?xml version="1.0"?>
  2. <report>
  3. <file type="cor" path="\external\density.cor">
  4. <surface>
  5. <a0>94,542439249332958</a0>
  6. <a1>-0,023660792953356</a1>
  7. <a2>-0,000247805755783</a2>
  8. <a11>0,000132167800465</a11>
  9. <a22>0,000029759193435</a22>
  10. <a12>-0,000000500686483</a12>
  11. <p>1,000000000000000</p>
  12. </surface>
  13. <surface>
  14. <a0>-3505,348808946265308</a0>
  15. <a1>3,842819162960808</a1>
  16. <a2>-0,253670976884668</a2>
  17. <a11>0,000000000000000</a11>
  18. <a22>0,000000000000000</a22>
  19. <a12>0,000000000000000</a12>
  20. <p>3,000000000000000</p>
  21. </surface>
  22. <surface>
  23. <a0>0,000000000000000</a0>
  24. <a1>0,000000000000000</a1>
  25. <a2>0,000000000000000</a2>
  26. <a11>-0,000577529221804</a11>
  27. <a22>0,000000234379777</a22>
  28. <a12>0,000000000000000</a12>
  29. <p>6,000000000000000</p>
  30. </surface>
  31. <surface>
  32. <a0>-20,620740405400667</a0>
  33. <a1>-0,008028079212385</a1>
  34. <a2>0,008291612967982</a2>
  35. <a11>0,000000000000000</a11>
  36. <a22>0,000000000000000</a22>
  37. <a12>0,000000000000000</a12>
  38. <p>7,000000000000000</p>
  39. </surface>
  40. <surface>
  41. <a0>1000,000000000000000</a0>
  42. <a1>0,000000000000000</a1>
  43. <a2>100,000000000000000</a2>
  44. <a11>0,100000000000000</a11>
  45. <a22>10,000000000000000</a22>
  46. <a12>0,100000000000000</a12>
  47. <p>5,000000000000000</p>
  48. </surface>
  49. </file>
  50. <file type="tar" path="\external\density.tar">
  51. <point>
  52. <x>1,001120947429713</x>
  53. <y>1,000000000000000</y>
  54. </point>
  55. <point>
  56. <x>1,007114858291929</x>
  57. <y>1,125000000000000</y>
  58. </point>
  59. <point>
  60. <x>1,013565787648818</x>
  61. <y>1,162999987602234</y>
  62. </point>
  63. <point>
  64. <x>1,016829740454785</x>
  65. <y>1,200000047683716</y>
  66. </point>
  67. <point>
  68. <x>1,021145746087924</x>
  69. <y>1,246999979019165</y>
  70. </point>
  71. <point>
  72. <x>1,025412597748493</x>
  73. <y>1,297000050544739</y>
  74. </point>
  75. <point>
  76. <x>1,031066554629070</x>
  77. <y>1,335000038146973</y>
  78. </point>
  79. <point>
  80. <x>1,034596220239890</x>
  81. <y>1,409999966621399</y>
  82. </point>
  83. <point>
  84. <x>1,041863525898852</x>
  85. <y>1,457999944686890</y>
  86. </point>
  87. <point>
  88. <x>1,047088434886263</x>
  89. <y>1,480000019073486</y>
  90. </point>
  91. <point>
  92. <x>1,048661341829451</x>
  93. <y>1,577999949455261</y>
  94. </point>
  95. <point>
  96. <x>1,058699113123393</x>
  97. <y>1,631999969482422</y>
  98. </point>
  99. <point>
  100. <x>1,064286037385592</x>
  101. <y>1,669999957084656</y>
  102. </point>
  103. <point>
  104. <x>1,067707114640065</x>
  105. <y>1,715000033378601</y>
  106. </point>
  107. <point>
  108. <x>1,072759073112358</x>
  109. <y>1,789999961853027</y>
  110. </point>
  111. <point>
  112. <x>1,079377092261531</x>
  113. <y>1,809000015258789</y>
  114. </point>
  115. <point>
  116. <x>1,080858893444794</x>
  117. <y>1,871000051498413</y>
  118. </point>
  119. <point>
  120. <x>1,086807401829741</x>
  121. <y>1,906000018119812</y>
  122. </point>
  123. </file>
  124. <file type="tar" path="\internal\rgr.tar">
  125. <point>
  126. <x>1,000000000000089</x>
  127. <y>96,203607227994326</y>
  128. </point>
  129. <point>
  130. <x>1,121647476309132</x>
  131. <y>64,153563926231982</y>
  132. </point>
  133. <point>
  134. <x>1,162617101433475</x>
  135. <y>57,084855874282766</y>
  136. </point>
  137. <point>
  138. <x>1,199997240030699</x>
  139. <y>71,170880906510774</y>
  140. </point>
  141. <point>
  142. <x>1,247032182620398</x>
  143. <y>47,598501400471378</y>
  144. </point>
  145. <point>
  146. <x>1,296018213060599</x>
  147. <y>45,497866121940660</y>
  148. </point>
  149. <point>
  150. <x>1,335486270278312</x>
  151. <y>43,918464200360177</y>
  152. </point>
  153. <point>
  154. <x>1,409007225812303</x>
  155. <y>39,454249422180510</y>
  156. </point>
  157. <point>
  158. <x>1,456475520177888</x>
  159. <y>35,629505963969677</y>
  160. </point>
  161. <point>
  162. <x>1,483019308050488</x>
  163. <y>34,892626423095862</y>
  164. </point>
  165. <point>
  166. <x>1,574686329729077</x>
  167. <y>35,219357108306212</y>
  168. </point>
  169. <point>
  170. <x>1,632079496130972</x>
  171. <y>27,344551293302175</y>
  172. </point>
  173. <point>
  174. <x>1,670156500480126</x>
  175. <y>25,494236532186278</y>
  176. </point>
  177. <point>
  178. <x>1,715145912653980</x>
  179. <y>24,320747575496625</y>
  180. </point>
  181. <point>
  182. <x>1,788382750490845</x>
  183. <y>22,830541923409328</y>
  184. </point>
  185. <point>
  186. <x>1,813709692183452</x>
  187. <y>21,144235339306761</y>
  188. </point>
  189. <point>
  190. <x>1,867528548449862</x>
  191. <y>22,706206620071953</y>
  192. </point>
  193. <point>
  194. <x>1,904989930225966</x>
  195. <y>20,091290930248761</y>
  196. </point>
  197. </file>
  198. <file type="tar" path="\external\rotate.tar">
  199. <point>
  200. <x>0,473827699164345</x>
  201. <y>0,000000000000001</y>
  202. </point>
  203. <point>
  204. <x>62,228428113975781</x>
  205. <y>-15,893077863579318</y>
  206. </point>
  207. <point>
  208. <x>113,650635808823651</x>
  209. <y>-15,791064565547854</y>
  210. </point>
  211. <point>
  212. <x>155,828331084435717</x>
  213. <y>-16,592300376397727</y>
  214. </point>
  215. <point>
  216. <x>205,995168819120522</x>
  217. <y>-17,666597648900968</y>
  218. </point>
  219. <point>
  220. <x>252,531471681222712</x>
  221. <y>-17,408675790553470</y>
  222. </point>
  223. <point>
  224. <x>309,677993283783906</x>
  225. <y>-19,025042962204655</y>
  226. </point>
  227. <point>
  228. <x>350,834621990577716</x>
  229. <y>-20,949042633494532</y>
  230. </point>
  231. </file>
  232. <file type="tar" path="\external\coriol.tar">
  233. <point>
  234. <x>100,000000000000000</x>
  235. <y>10,000000000000000</y>
  236. </point>
  237. </file>
  238. </report>

Решение задачи: «Как создать DataTable каждый раз когда меняется значение атрибута в xml файле или как мне посчитать количество»

textual
Листинг программы
  1. public void  ReadXml(string pathXmlFile)
  2. {
  3.     List<DataTable> listDt = new List<DataTable>();
  4.     XDocument xDoc = XDocument.Load(pathXmlFile);
  5.     foreach (XElement el in xDoc.Root.Elements())
  6.     {
  7.         XAttribute pathAttr = el.Attribute("path");
  8.         if (pathAttr == null || pathAttr.Value == @"\external\density.cor") continue;
  9.        
  10.         if (el.Element("surface") != null)
  11.         {
  12.             listDt.Add(ReadSurfaceTable(el));
  13.         }
  14.         else if (el.Element("point") != null)
  15.         {
  16.             listDt.Add(ReadPointTable(el));
  17.         }
  18.     }
  19.    
  20.     listDt.Dump();
  21. }
  22.  
  23. DataTable ReadSurfaceTable(XElement el)
  24. {
  25.     DataTable table = new DataTable();
  26.     table.Columns.Add(new DataColumn("a0", typeof(Double)));
  27.     table.Columns.Add(new DataColumn("a1", typeof(Double)));
  28.     table.Columns.Add(new DataColumn("a2", typeof(Double)));
  29.     table.Columns.Add(new DataColumn("a11", typeof(Double)));
  30.     table.Columns.Add(new DataColumn("a22", typeof(Double)));
  31.     table.Columns.Add(new DataColumn("a12", typeof(Double)));
  32.     //table.Columns.Add(new DataColumn("p", typeof(Double)));
  33.     foreach (XElement childEl in el.Elements())
  34.     {
  35.         DataRow row = table.NewRow();
  36.         row["a0"] = double.Parse(childEl.Element("a0").Value);
  37.         row["a1"] = double.Parse(childEl.Element("a1").Value);
  38.         row["a2"] = double.Parse(childEl.Element("a2").Value);
  39.         row["a11"] = double.Parse(childEl.Element("a11").Value);
  40.         row["a22"] = double.Parse(childEl.Element("a22").Value);
  41.         row["a12"] = double.Parse(childEl.Element("a12").Value);
  42.         //row["p"] = double.Parse(childEl.Element("p").Value);
  43.         table.Rows.Add(row);
  44.     }
  45.     return table;
  46. }
  47.  
  48. DataTable ReadPointTable(XElement el)
  49. {
  50.     DataTable table = new DataTable();
  51.     table.Columns.Add(new DataColumn("x", typeof(Double)));
  52.     table.Columns.Add(new DataColumn("y", typeof(Double)));
  53.     foreach (XElement childEl in el.Elements())
  54.     {
  55.         DataRow row = table.NewRow();
  56.         row["x"] = double.Parse(childEl.Element("x").Value);
  57.         row["y"] = double.Parse(childEl.Element("y").Value);
  58.         table.Rows.Add(row);
  59.     }
  60.     return table;
  61. }

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

12   голосов , оценка 4.417 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут
Похожие ответы