在读取GPX / XML文件时转换数据

时间:2016-06-20 17:03:50

标签: c# casting

我想从featureset文件(GPX)创建XML。 构建项目时,double lat = Double.Parse(excelRow["lat"].ToString());

上会显示一个转换异常
        DataTable table = new DataTable();
        table.Columns.Add("lat", typeof(string));
        table.Columns.Add("long", typeof(string));

        // Iterate on the node set
        listBox1.Items.Clear();
        try
        {
            while (iteratorLat.MoveNext() && iteratorLong.MoveNext())
            {
                XPathNavigator navLat = iteratorLat.Current.Clone();
                listBox1.Items.Add(navLat.Value);

                XPathNavigator navLong = iteratorLong.Current.Clone();
                listBox1.Items.Add(navLong.Value);

                table.Rows.Add(navLong.Value, navLat.Value);
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
        dataGridView1.DataSource = table;
        dataGridView1.Show();

        // Création de la couche

        var featureSet = ConvertDataTableToFeatureSet(table);

        if (featureSet != null)
        {
            //add feature set to map
            var layer = map1.Layers.Add(featureSet);
            layer.LegendText = "Données GPX";
        }

代码的第二部分(其中转换不起作用)

        private static FeatureSet ConvertDataTableToFeatureSet(DataTable excelTable)
    {
        // See if table has the lat, long columns
        if (excelTable.Columns.Contains("lat") & excelTable.Columns.Contains("long"))
        {
            MessageBox.Show("Continue");
            FeatureSet fs = new FeatureSet(FeatureType.Point);
            fs.Projection = KnownCoordinateSystems.Geographic.World.WGS1984;

            // Set columns of attribute table
            fs.DataTable = excelTable.Clone();

            foreach (DataRow excelRow in excelTable.Rows)
            {
                MessageBox.Show("Continue..");
                double lat = Double.Parse(excelRow["lat"].ToString());
                double lon = Double.Parse(excelRow["long"].ToString());

                // Add the point to the FeatureSet
                Coordinate coord = new Coordinate(lon, lat);
                DotSpatial.Topology.Point point = new DotSpatial.Topology.Point(coord);
                IFeature feature = fs.AddFeature(point);

                // Bring over all of the data as attribute data.
                for (int i = 0; i <= excelTable.Columns.Count - 1; i++)
                {
                    feature.DataRow[i] = excelRow[i];
                }
            }
            return fs;
        }
        else
        {
            MessageBox.Show("La table doit contenir les champs \"lat\" et \"long\"");
            return null;
        }
    }

0 个答案:

没有答案