Click here to Skip to main content
15,908,445 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
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,10]    0.000976563 0.000976563 0.000976563 0.000976563 0.000976563
y\x  [2,15]    0.000976563 0.000976563 0.000976563 0.000976563 0.000976563
y\x  [4,30]    0.000976563 0.000976563 0.000976563 0.000976563 0.000976563
y\x  [6,40]    0.000976563 0.000976563 0.000976563 0.000976563 0.000976563
y\x  [8,55]    0.000976563 0.000976563 0.000976563 0.000976563 0.000976563

The structure of my XML file is:
XML
  <?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:
C#
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,
Yuri
Posted
Updated 18-Feb-14 12:17pm
v2
Comments
idenizeni 18-Feb-14 20:45pm    
I think OriginalGriffs solution should work for you. However, I believe you need to look at your query because the results are probably empty. The XML you posted it not properly formed as you have a start element of Parameter and an end element of Parameter1. I'm guessing this is only in what you posted and not your actual XML but you may need to verify your XML is well-formed.

1 solution

Can't you just replace the foreach loop with:
C#
datagridview1.DataSource=variables;
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900