通过foreach循环添加几个元素

时间:2014-04-14 11:57:10

标签: c# asp.net foreach

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

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

代码:

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

    }

0 个答案:

没有答案