简体   繁体   中英

Convert string to multi dimensional array

I am parsing an XML file and have hard coded a method to tear apart a string to create a multidimensional array. Here's a fragment of the XML I am parsing:

<MeasPropList Path="userList" Type="System.Double[]">
    {1960, 1980, 0}, {1980, 0, 0}, {1960, 1980, 1990}
</MeasPropList>

The <MeasPropList> element is used for any kind of data, not just arrays or numeric. The equivalent result of this XML in C# is:

double[,] userList = new[,] { { 1960.0, 1980.0, 0.0 }, 
                       { 1980.0, 0.0, 0.0 }, 
                       { 1960.0, 1980.0, 1990.0 } };

I'll spare you the code I am using, it works ok but I am looking for a more elegant solution.
Is there a way I can use Array.ConvertAll<> ?
Is there an XML library function I can use to parse the XML numeric data?

[EDIT] I posted the code I am using here:
https://codereview.stackexchange.com/questions/15922/parsing-xml-to-create-ilist

If you're not fixed on that format, you could use jagged arrays and an XmlSerializer:

    double[][][] d = new double[][][] { new double[][] {
                                            new double[] {1.0, 2.0, 3.0},
                                            new double[] {4.0, 5.0, 6.0},
                                            new double[] {7.0, 8.0, 9.0}
                                        },
                                        new double[][] {
                                            new double[] {10.0, 11.0, 12.0},
                                            new double[] {13.0, 14.0, 15.0},
                                            new double[] {16.0, 17.0, 18.0}
                                        },
                                        new double[][] {
                                            new double[] {19.0, 20.0, 21.0},
                                            new double[] {22.0, 23.0, 24.0},
                                            new double[] {25.0, 26.0 ,27.0}
                                        }
                                    };

    XmlSerializer x = new XmlSerializer(typeof(double[][][]));

    using (StringWriter sw = new StringWriter())
    {
        x.Serialize(sw, d);
        Console.WriteLine(sw.ToString());
    }

That yields the following XML:

<?xml version="1.0" encoding="utf-16"?>
<ArrayOfArrayOfArrayOfDouble xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <ArrayOfArrayOfDouble>
    <ArrayOfDouble>
      <double>1</double>
      <double>2</double>
      <double>3</double>
    </ArrayOfDouble>
    <ArrayOfDouble>
      <double>4</double>
      <double>5</double>
      <double>6</double>
    </ArrayOfDouble>
    <ArrayOfDouble>
      <double>7</double>
      <double>8</double>
      <double>9</double>
    </ArrayOfDouble>
  </ArrayOfArrayOfDouble>
  <ArrayOfArrayOfDouble>
    <ArrayOfDouble>
      <double>10</double>
      <double>11</double>
      <double>12</double>
    </ArrayOfDouble>
    <ArrayOfDouble>
      <double>13</double>
      <double>14</double>
      <double>15</double>
    </ArrayOfDouble>
    <ArrayOfDouble>
      <double>16</double>
      <double>17</double>
      <double>18</double>
    </ArrayOfDouble>
  </ArrayOfArrayOfDouble>
  <ArrayOfArrayOfDouble>
    <ArrayOfDouble>
      <double>19</double>
      <double>20</double>
      <double>21</double>
    </ArrayOfDouble>
    <ArrayOfDouble>
      <double>22</double>
      <double>23</double>
      <double>24</double>
    </ArrayOfDouble>
    <ArrayOfDouble>
      <double>25</double>
      <double>26</double>
      <double>27</double>
    </ArrayOfDouble>
  </ArrayOfArrayOfDouble>
</ArrayOfArrayOfArrayOfDouble>

Here's a stab:

    static void Main(string[] args)
    {
        var parser = new MeasPropListParser<double>();
        var values = parser.GetValues("{1960, 1980, 0}, {1980, 0, 0}, {1960, 1980, 1990}");
    }


   public class MeasPropListParser<T>
    {
        private char[] comma = new char[] { ',' };
        private char[] rightBrace = new char[] { '}' };

        public T[,] GetValues(string input)        
        {
            var dims = GetDimensions(input);
            int rowCount = dims.Item1;
            int colCount = dims.Item2;

            T[,] array = new T[rowCount, colCount];
            var converter = System.ComponentModel.TypeDescriptor.GetConverter(typeof(T));
            var rows = GetRows(input);

            for (int x = 0; x < rowCount; x++)
            {
                var cols = new List<string>(rows[x].Split(comma, StringSplitOptions.RemoveEmptyEntries));
                for (int y = 0; y < colCount; y++)
                {
                    array[x, y] = (T)converter.ConvertFromString(cols[y]);
                }

            }

            return array;
        }

        private Tuple<int, int> GetDimensions(string input)
        {
            int rowCount = 0;
            int colCount = 0;
            foreach (var array in input.Trim().Split(rightBrace, StringSplitOptions.RemoveEmptyEntries))
            {
                rowCount++;
                if (colCount == 0)
                {
                    colCount = array.Split(comma, StringSplitOptions.RemoveEmptyEntries).Length;
                }
            }
            return new Tuple<int, int>(rowCount, colCount);
        }

        private List<string> GetRows(string input)
        {
            var list = new List<string>();
            foreach (var array in input.Trim().Split(rightBrace, StringSplitOptions.RemoveEmptyEntries))
            {
                list.Add(array.Replace("{", "").Trim());
            }
            return list;
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM