簡體   English   中英

將字符串轉換為多維數組

[英]Convert string to multi dimensional array

我正在解析XML文件,並已硬編碼了一種方法,可以將字符串拆開以創建多維數組。 這是我正在解析的XML片段:

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

<MeasPropList>元素用於任何類型的數據,而不僅僅是數組或數字。 C#中此XML的等效結果是:

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

我將為您省去我正在使用的代碼,它可以正常工作,但我正在尋找更優雅的解決方案。
有沒有一種方法可以使用Array.ConvertAll <>?
我可以使用XML庫函數來解析XML數字數據嗎?

[編輯]我在這里發布了我正在使用的代碼:
https://codereview.stackexchange.com/questions/15922/parsing-xml-to-create-ilist

如果不確定該格式,則可以使用鋸齒狀數組和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());
    }

產生以下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>

這是一個刺:

    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;
        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM