C# 通过foreach循环添加几个元素

C# 通过foreach循环添加几个元素,c#,asp.net,foreach,C#,Asp.net,Foreach,我试图解析一个双精度数字数组,而不是静态字符串,就像我在这里测试的那样,我将在文本框中输入数字,单击按钮后,它将从数据库中选择所有相关坐标。所以我需要解析很多对坐标(lat,lon),我尝试了foreach循环,但在最后第三行无法理解。数据库中字段的名称是经度和纬度 new XElement(n + "Linestring", new XElement(n + "Coordinates", lat+","+lon)))))); 代码: 如果你想要任何有用的答案,你应该解释你的实际问题是什么。它

我试图解析一个双精度数字数组,而不是静态字符串,就像我在这里测试的那样,我将在文本框中输入数字,单击按钮后,它将从数据库中选择所有相关坐标。所以我需要解析很多对坐标(lat,lon),我尝试了foreach循环,但在最后第三行无法理解。数据库中字段的名称是经度和纬度

new XElement(n + "Linestring", new XElement(n + "Coordinates", lat+","+lon))))));
代码:


如果你想要任何有用的答案,你应该解释你的实际问题是什么。它是否抛出错误(如果是,是编译时还是运行时)?我在发布的代码中也没有看到foreach循环,因此不清楚您实际上在尝试什么。在存储纬度/经度方面,十进制优于双精度。
protected void Page_Load(object sender, EventArgs e)
    {
        //String[] values = {"12.1", "2.1","3.1","2.1" };
        String values = "12.1,2.1,3.1,1.1";
        String[] latLon = values.Split(',');

        SqlConnection sqlcon = new SqlConnection(conStr);
        // String com = "select Latitude, Longitude from Coordinates where IMEI=@txtIMEI";
        SqlCommand sqlcom = new SqlCommand("GetLatLon", sqlcon);
        sqlcom.CommandType = CommandType.StoredProcedure;
        sqlcom.Parameters.Add("@IMEI", SqlDbType.VarChar).Value = TextBox1.Text;

        DataTable dt = new DataTable();
        SqlDataAdapter sda = new SqlDataAdapter(sqlcom);
        sda.Fill(dt);

        try
        {
            sqlcon.Open();
            sqlcom.ExecuteNonQuery();


                double lon = double.Parse(latLon[0]);
                double lat = double.Parse(latLon[1]);
                Response.Write(lon + "," + lat);

                XNamespace n = "http://earth.google.com/kml/2.2";
                Response.Write("KML Generated");
                new XElement(n + "kml");//just do n+ for each underlying elements
                //  XNamespace n = "http://earth.google.com/kml/2.2";
                XDocument doc = new XDocument(
                new XDeclaration("1.0", "utf-8", ""),
                new XComment("This is comment by me"),
                new XElement(n + "kml",
                new XElement(n + "Document",
                        new XElement(n + "Name", "something"), new XElement(n + "Placemark",
                        new XAttribute("id", "1"),
                        new XElement(n + "title", "something"),
                        new XElement(n + "description", "something"),
                        new XElement(n + "LookAt",
                        new XElement(n + "Longitude", "12.1"),
                        new XElement(n + "Latitude", "2.1")),
                        new XElement(n + "Linestring", new XElement(n + "Coordinates", lat+","+lon))))));
                        doc.Save(Server.MapPath(@"~\marker5.kml"));
                        Response.Write("KML Generated");



        }
        catch (Exception exc)
        {
            Response.Write(exc.Message);
        }
        finally
        {
            sqlcon.Close();
        }
    }

    }