c#读xml之kml文件

后台:
                HttpFileCollection xmlfile = null;
		xmlfile = context.Request.Files;
                if (xmlfile != null && xmlfile.Count > 0)
                {
                    string guid = Guid.NewGuid().ToString();
                    string uploadDateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                    string save_Path = context.Server.MapPath("../upload/KML/");
                    HttpPostedFile file = xmlfile[0];
                    string newXmlFileName = curDataTime + "-" + guid.ToUpper().Replace("-", "") + "-" + file.FileName;
                    file.SaveAs(save_Path + newXmlFileName);
                    //上传成功后,读kml文件内容,将内容导保存到数据库中
                    {
                        XmlDocument xmldoc = new XmlDocument();
                        xmldoc.Load(save_Path + newXmlFileName);
                        XmlElement root = xmldoc.DocumentElement;
                        /规范命名空间
                        XmlNode _document = root.GetElementsByTagName("Document")[0];
                        XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmldoc.NameTable);
                        if (_document.Attributes["xmlns"]==null)
                        {
                            nsmgr.AddNamespace("ns", root.Attributes["xmlns"].Value);
                        }
                        else
                        {
                            nsmgr.AddNamespace("ns", _document.Attributes["xmlns"].Value);
                        }
                        XmlNodeList xmlmark = root.GetElementsByTagName("Placemark");
                        {
                            int tpid = 0;//踏勘点序号                       
                            for (int m = 2; m < xmlmark.Count - 1; m++)//前两个是起点和终点不插入数据库
                            {
                                tpid = tpid + 1;
                                XmlNode nameNode = xmlmark[m].SelectSingleNode("ns:name", nsmgr);
                                string nmNode = nameNode.InnerText;
                                string note = "";
                                if (nmNode.IndexOf(" ") > 0)//存在空格,就说明有备注
                                {
                                    note = nmNode.Substring(nmNode.IndexOf(" ") + 1);
                                }
                                XmlNodeList divList = xmlmark[m].SelectNodes("ns:description/ns:div", nsmgr);

                                string lng = (divList[1].InnerText).Substring(3);
                                string lat = (divList[2].InnerText).Substring(3);
                                string lnglat = lng + "," + lat;
                                string ph = context.Server.MapPath("../upload/");
                                string path = curDataTime + "/TankanData/Image" + lng + lat+"/";
                                Directory.CreateDirectory(ph + path);
                                XmlNodeList a_img = divList[0].SelectNodes("ns:a/ns:img", nsmgr);
                                string src = "";
                                string path_name = projectName + "-" + fangan + "-" + tpid + "号踏勘点";
                                string img_path = "";
                                string img_name = "";
                                for (int n = 0; n < a_img.Count; n++)
                                {
                                    src = a_img[n].Attributes["src"].Value;//photo
                                    string save_path = path + path_name + "-" + (n + 1) + ".jpg";
                                    GetImg(src, ph + save_path);
                                    img_name += img_name + path_name + "-" + (n + 1) + ".jpg;";
                                    img_path = "/upload/" + path + img_name.Remove(img_name.Length-1);
                                   
                                }
                                string sql2 = "";
                                sql2 = "insert into Table_MarkerInfo (accordinate,uploadDate)values ('" + lnglat + uploadDateTime + "')";
                                DBHelper.ExecuteCommand(sql2);
                            }
                            //获取轨迹,插入数据库
                            XmlNodeList node = root.GetElementsByTagName("gx:coord");
                            string arr = "";
                            for (int i = 0; i < node.Count; i++)
                            {
                                string str = node[i].InnerXml;
                                string[] pt = str.Split(' ');
                                for (int j = 0; j < pt.Length - 1; j++)
                                {
                                    arr += pt[j] + ",";
                                }
                            }
                            arr = arr.Substring(0, arr.Length - 1);
                            string sql = "";
                            string sql5 = "";
                            string sql1 = "select company from Table_User where id='" + userid + "'";
                            DataTable dt1 = DBHelper.GetDataSet(sql1).Tables[0];
                            sql = "insert into Table (guiji,uploadDate)values('" + arr + "','" + uploadDateTime + "')";
                            DBHelper.ExecuteCommand(sql);
                        }
                    }
                }

你可能感兴趣的:(后台,xml,kml,c#,.net,aspx)