0

我正在解析一个 XML 文件,并硬编码了一种方法来拆分字符串以创建多维数组。这是我正在解析的 XML 片段:

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

<MeasPropList> 元素可用于任何类型的数据,而不仅仅是数组或数字。此 XML 在 C# 中的等效结果是:

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

4

2 回答 2

0

这是一个刺:

    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;
        }
    }
于 2012-09-21T20:15:40.807 回答
0

如果您不固定该格式,则可以使用锯齿状数组和 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>
于 2012-09-21T20:05:01.883 回答