Malush Yuriy

Malush Yuriy

  • NA
  • 1
  • 793

Write multi demensional Array from xml to DataGridView

Feb 18 2014 6:13 PM
Good Evening.I have q question to you.Maybe you can help me.How can I load a Data from XML file to DataGridView like the table below:
y\x [0,0] 0.000976563 0.000976563 0.000976563 0.000976563 0.000976563
y\x [10,10] 0.000976563 0.000976563 0.000976563 0.000976563 0.000976563
y\x [20,20] 0.000976563 0.000976563 0.000976563 0.000976563 0.000976563
y\x [30,30] 0.000976563 0.000976563 0.000976563 0.000976563 0.000976563
y\x [40,40] 0.000976563 0.000976563 0.000976563 0.000976563 0.000976563
The structure of my XML file is:

<?xml version="1.0" standalone="yes"?> <Tag> <Parameter> <Name>Parameter1</Name> <Size_X>7</Size_X> <Size_Y>2</Size_Y> <Value_X>0;2;8;12;14;16;19;</Value_X> <Value_Y>-20;-10;</Value_Y> <Value>0000000000000;5.0000000000000000;10.0000000000000000;15.0000000000000000;20.0000000000000000;25.0000000000000000;0000000000000;5.0000000000000000;10.0000000000000000;15.0000000000000000;20.0000000000000000;25.0000000000000000;</Value> </Parameter1> <Parameter2> <Name>Parameter2</Name> <Size_X>7</Size_X> <Size_Y>3</Size_Y> <Value_X>0;2;4;6;8;10;12;</Value_X> <Value_Y>20;40;60;</Value_Y> <Value>0000000000000;5.0000000000000000;10.0000000000000000;15.0000000000000000;20.0000000000000000;30.0000000000000000; 0000000000000;5.0000000000000000;10.0000000000000000;15.0000000000000000;20.0000000000000000;30.0000000000000000;0000000000000;5.0000000000000000;10.0000000000000000;15.0000000000000000;20.0000000000000000;30.0000000000000000;</Value> </Parameter2> </Tag> I Have tried the next code, but it does not work:
XDocument xmlDoc = XDocument.Load("C:\\Write.xml");                
 var variables = from variable in xmlDoc.Descendants("Parameter")                           
      where variable.Element("Name").Value == "Parameter1"                                
 select new                                 {                                   
  Name = variable.Element("Name").Value,                                    
 SizeX = variable.Element("Size_X").Value,                                     
SizeY = variable.Element("Size_Y").Value,                                     
ValueX = variable.Element("Value_X").Value,                                    
 ValueY = variable.Element("Value_Y").Value,                                     
Value = variable.Element("Value").Value,                                 
};                   foreach (var variable in variables)                
 {                       var x = Convert.ToInt32(variable.SizeX);                 
    var y = Convert.ToInt32(variable.SizeY);                     
dataGridView1.TopLeftHeaderCell.Value = "y/x";                       
var arr = new double[y, x];//{Value} it must be the value here???                
     var columnCount = arr.GetUpperBound(1) + 1;                    
 var rowCount = arr.GetUpperBound(0) + 1;                     
  for (int i = 0; i < columnCount; i++)                     
{                         dataGridView1.Columns.Add(i.ToString(), variable.ValueX);                     
}            
         for (int i = 0; i < rowCount; i++)                  
   {                           dataGridView1.Rows.Add(i.ToString(),variable.ValueY);                 
        for (int k = 0; k < columnCount; k++)                        
 {                             dataGridView1.Rows[i].Cells[k].Value = arr[i, k];     
}    
                   }   
Thanks a lot for your Answer.
 Best Regards,
Malush