Как создать DataTable каждый раз когда меняется значение атрибута в xml файле или как мне посчитать количество - C#
Формулировка задачи:
Вот пример для одного DataTable, а мне надо сделать List<DataTable> - ов, когда меняется значение атрибута path. А пока у меня все точки point записывает в одну таблицу DataTable.
private DataTable TablePoint()
{
//создаём таблицу
DataTable dtPoint = new DataTable();
//создаём три колонки
DataColumn x = new DataColumn("x", typeof(Double));
DataColumn y = new DataColumn("y", typeof(Double));
//добавляем колонки в таблицу
dtPoint.Columns.Add(x);
dtPoint.Columns.Add(y);
return dtPoint;
}
listDt.Add(TablePoint());
DataRow newRowListDt = null;
foreach (XElement elm in xDoc.Descendants("point"))
{
newRowListDt = listDt[0].NewRow();
if (elm.Element("x") != null)
{
newRowListDt["x"] = double.Parse(elm.Element("x").Value);
}
if (elm.Element("y") != null)
{
newRowListDt["y"] = double.Parse(elm.Element("y").Value);
}
listDt[0].Rows.Add(newRowListDt);
}<?xml version="1.0"?>
<report>
<file type="cor" path="\external\density.cor">
<surface>
<a0>94,542439249332958</a0>
<a1>-0,023660792953356</a1>
<a2>-0,000247805755783</a2>
<a11>0,000132167800465</a11>
<a22>0,000029759193435</a22>
<a12>-0,000000500686483</a12>
<p>1,000000000000000</p>
</surface>
<surface>
<a0>-3505,348808946265308</a0>
<a1>3,842819162960808</a1>
<a2>-0,253670976884668</a2>
<a11>0,000000000000000</a11>
<a22>0,000000000000000</a22>
<a12>0,000000000000000</a12>
<p>3,000000000000000</p>
</surface>
<surface>
<a0>0,000000000000000</a0>
<a1>0,000000000000000</a1>
<a2>0,000000000000000</a2>
<a11>-0,000577529221804</a11>
<a22>0,000000234379777</a22>
<a12>0,000000000000000</a12>
<p>6,000000000000000</p>
</surface>
<surface>
<a0>-20,620740405400667</a0>
<a1>-0,008028079212385</a1>
<a2>0,008291612967982</a2>
<a11>0,000000000000000</a11>
<a22>0,000000000000000</a22>
<a12>0,000000000000000</a12>
<p>7,000000000000000</p>
</surface>
<surface>
<a0>1000,000000000000000</a0>
<a1>0,000000000000000</a1>
<a2>100,000000000000000</a2>
<a11>0,100000000000000</a11>
<a22>10,000000000000000</a22>
<a12>0,100000000000000</a12>
<p>5,000000000000000</p>
</surface>
</file>
<file type="tar" path="\external\density.tar">
<point>
<x>1,001120947429713</x>
<y>1,000000000000000</y>
</point>
<point>
<x>1,007114858291929</x>
<y>1,125000000000000</y>
</point>
<point>
<x>1,013565787648818</x>
<y>1,162999987602234</y>
</point>
<point>
<x>1,016829740454785</x>
<y>1,200000047683716</y>
</point>
<point>
<x>1,021145746087924</x>
<y>1,246999979019165</y>
</point>
<point>
<x>1,025412597748493</x>
<y>1,297000050544739</y>
</point>
<point>
<x>1,031066554629070</x>
<y>1,335000038146973</y>
</point>
<point>
<x>1,034596220239890</x>
<y>1,409999966621399</y>
</point>
<point>
<x>1,041863525898852</x>
<y>1,457999944686890</y>
</point>
<point>
<x>1,047088434886263</x>
<y>1,480000019073486</y>
</point>
<point>
<x>1,048661341829451</x>
<y>1,577999949455261</y>
</point>
<point>
<x>1,058699113123393</x>
<y>1,631999969482422</y>
</point>
<point>
<x>1,064286037385592</x>
<y>1,669999957084656</y>
</point>
<point>
<x>1,067707114640065</x>
<y>1,715000033378601</y>
</point>
<point>
<x>1,072759073112358</x>
<y>1,789999961853027</y>
</point>
<point>
<x>1,079377092261531</x>
<y>1,809000015258789</y>
</point>
<point>
<x>1,080858893444794</x>
<y>1,871000051498413</y>
</point>
<point>
<x>1,086807401829741</x>
<y>1,906000018119812</y>
</point>
</file>
<file type="tar" path="\internal\rgr.tar">
<point>
<x>1,000000000000089</x>
<y>96,203607227994326</y>
</point>
<point>
<x>1,121647476309132</x>
<y>64,153563926231982</y>
</point>
<point>
<x>1,162617101433475</x>
<y>57,084855874282766</y>
</point>
<point>
<x>1,199997240030699</x>
<y>71,170880906510774</y>
</point>
<point>
<x>1,247032182620398</x>
<y>47,598501400471378</y>
</point>
<point>
<x>1,296018213060599</x>
<y>45,497866121940660</y>
</point>
<point>
<x>1,335486270278312</x>
<y>43,918464200360177</y>
</point>
<point>
<x>1,409007225812303</x>
<y>39,454249422180510</y>
</point>
<point>
<x>1,456475520177888</x>
<y>35,629505963969677</y>
</point>
<point>
<x>1,483019308050488</x>
<y>34,892626423095862</y>
</point>
<point>
<x>1,574686329729077</x>
<y>35,219357108306212</y>
</point>
<point>
<x>1,632079496130972</x>
<y>27,344551293302175</y>
</point>
<point>
<x>1,670156500480126</x>
<y>25,494236532186278</y>
</point>
<point>
<x>1,715145912653980</x>
<y>24,320747575496625</y>
</point>
<point>
<x>1,788382750490845</x>
<y>22,830541923409328</y>
</point>
<point>
<x>1,813709692183452</x>
<y>21,144235339306761</y>
</point>
<point>
<x>1,867528548449862</x>
<y>22,706206620071953</y>
</point>
<point>
<x>1,904989930225966</x>
<y>20,091290930248761</y>
</point>
</file>
<file type="tar" path="\external\rotate.tar">
<point>
<x>0,473827699164345</x>
<y>0,000000000000001</y>
</point>
<point>
<x>62,228428113975781</x>
<y>-15,893077863579318</y>
</point>
<point>
<x>113,650635808823651</x>
<y>-15,791064565547854</y>
</point>
<point>
<x>155,828331084435717</x>
<y>-16,592300376397727</y>
</point>
<point>
<x>205,995168819120522</x>
<y>-17,666597648900968</y>
</point>
<point>
<x>252,531471681222712</x>
<y>-17,408675790553470</y>
</point>
<point>
<x>309,677993283783906</x>
<y>-19,025042962204655</y>
</point>
<point>
<x>350,834621990577716</x>
<y>-20,949042633494532</y>
</point>
</file>
<file type="tar" path="\external\coriol.tar">
<point>
<x>100,000000000000000</x>
<y>10,000000000000000</y>
</point>
</file>
</report>
Решение задачи: «Как создать DataTable каждый раз когда меняется значение атрибута в xml файле или как мне посчитать количество»
textual
Листинг программы
public void ReadXml(string pathXmlFile)
{
List<DataTable> listDt = new List<DataTable>();
XDocument xDoc = XDocument.Load(pathXmlFile);
foreach (XElement el in xDoc.Root.Elements())
{
XAttribute pathAttr = el.Attribute("path");
if (pathAttr == null || pathAttr.Value == @"\external\density.cor") continue;
if (el.Element("surface") != null)
{
listDt.Add(ReadSurfaceTable(el));
}
else if (el.Element("point") != null)
{
listDt.Add(ReadPointTable(el));
}
}
listDt.Dump();
}
DataTable ReadSurfaceTable(XElement el)
{
DataTable table = new DataTable();
table.Columns.Add(new DataColumn("a0", typeof(Double)));
table.Columns.Add(new DataColumn("a1", typeof(Double)));
table.Columns.Add(new DataColumn("a2", typeof(Double)));
table.Columns.Add(new DataColumn("a11", typeof(Double)));
table.Columns.Add(new DataColumn("a22", typeof(Double)));
table.Columns.Add(new DataColumn("a12", typeof(Double)));
//table.Columns.Add(new DataColumn("p", typeof(Double)));
foreach (XElement childEl in el.Elements())
{
DataRow row = table.NewRow();
row["a0"] = double.Parse(childEl.Element("a0").Value);
row["a1"] = double.Parse(childEl.Element("a1").Value);
row["a2"] = double.Parse(childEl.Element("a2").Value);
row["a11"] = double.Parse(childEl.Element("a11").Value);
row["a22"] = double.Parse(childEl.Element("a22").Value);
row["a12"] = double.Parse(childEl.Element("a12").Value);
//row["p"] = double.Parse(childEl.Element("p").Value);
table.Rows.Add(row);
}
return table;
}
DataTable ReadPointTable(XElement el)
{
DataTable table = new DataTable();
table.Columns.Add(new DataColumn("x", typeof(Double)));
table.Columns.Add(new DataColumn("y", typeof(Double)));
foreach (XElement childEl in el.Elements())
{
DataRow row = table.NewRow();
row["x"] = double.Parse(childEl.Element("x").Value);
row["y"] = double.Parse(childEl.Element("y").Value);
table.Rows.Add(row);
}
return table;
}