Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Nov 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    c# - streamwriter and datagridview

    Hi All,
    i need your assistance with the following:
    i have built an application that fills inputs from a database to a datagridview.
    the applicaiton then takes the inputs from the datagridview and writes it down to a file (kml --> google earth).
    i know how to write to a file the issue is the order.
    the output of the file should be:

    Code:
    <Placemark>
    			<name>Alarm Zone 2</name>
    			<styleUrl>#m_ylw-pushpin</styleUrl>
    			<Polygon>
    				<tessellate>1</tessellate>
    				<outerBoundaryIs>
    					<LinearRing>
    						<coordinates>
    							-101.2023177342566,35.92269094626265,0 -101.2022793432744,35.92253750459201,0 -101.2020270554968,35.92258752157488,0 -101.2020657349624,35.92275793800641,0 -101.2023177342566,35.92269094626265,0 
    						</coordinates>
    					</LinearRing>
    				</outerBoundaryIs>
    			</Polygon>
    currently i managed to wirte the first part of the file
    Code:
    <Placemark>
    			<name>Alarm Zone 2</name>
    			<styleUrl>#m_ylw-pushpin</styleUrl>
    			<Polygon>
    				<tessellate>1</tessellate>
    				<outerBoundaryIs>
    					<LinearRing>
    						<coordinates>
    							-101.2023177342566,35.92269094626265,0 -101.2022793432744,35.92253750459201,0 -101.2020270554968,35.92258752157488,0 -101.2020657349624,35.92275793800641,0 -101.2023177342566,35.92269094626265,0
    the issue i have is with the second part of the syntax:
    Code:
    						</coordinates>
    					</LinearRing>
    				</outerBoundaryIs>
    			</Polygon>
    		</Placemark>
    no matter where i put
    Code:
                    KMLwriter1.WriteLine("\r\n");
                    KMLwriter1.WriteLine("</coordinates>" + "\r\n");
                    KMLwriter1.WriteLine("</LinearRing>" + "\r\n");
                    KMLwriter1.WriteLine("</outerBoundaryIs>" + "\r\n");
                    KMLwriter1.WriteLine("</Polygon>" + "\r\n");
                    KMLwriter1.WriteLine("</Placemark>" + "\r\n");
    i mess up the file and i get something like this
    Code:
    <Placemark>
    
    <name>External_1_AL</name>
    
    <styleUrl>#m_ylw-pushpin</styleUrl>
    
    <Polygon>
    
    <tessellate>1</tessellate>
    
    <outerBoundaryIs>
    
    <LinearRing>
    
    <coordinates>
    
    -101.2023177342566,35.92269094626265,0
    
    </coordinates>
    
    </LinearRing>
    
    </outerBoundaryIs>
    
    </Polygon>
    
    </Placemark>
    
     -101.2022793432744,35.92253750459201,0
    below you can find my current code.

    Code:
          #region Class LatLongPair
            class LatLongPair
            {
                public double  Lat { get;set; }
                public double  Lon { get;set; }
            }
            #endregion
    
    
            private void button3_Click(object sender, EventArgs e)
            {
    
                StreamWriter KMLwriter1 = new System.IO.StreamWriter(KMLDestinationTB.Text + "\\" + sitenameTB.Text + ".kml", false);
    
                #region KML Header Writings
    
    
    
    
                #endregion KML Header Writing
    
    
    
    
                #region testing new code from web
    
                // this is a new code from the web
    
                Dictionary<string, List<LatLongPair>> LatLongCollectionByName = new Dictionary<string, List<LatLongPair>>();
    
                List<LatLongPair> lst = null;
                LatLongPair lPair = null;
                string matchHandle = string.Empty;
    
                for (int i = 0; i < LayersGrid.Rows.Count; i++)
                {
                    try
                    {
                        if (LayersGrid.Rows[i].Cells[0].Value.ToString() == matchHandle)
                        {
                            lPair.Lat = Convert.ToDouble(LayersGrid.Rows[i].Cells[2].Value);
                            lPair.Lon = Convert.ToDouble(LayersGrid.Rows[i].Cells[1].Value);
                            lst.Add(lPair);
                        }
    
                        else
                        {
                            matchHandle = LayersGrid.Rows[i].Cells[0].Value.ToString();
                            lPair = new LatLongPair();
                            lPair.Lat = Convert.ToDouble(LayersGrid.Rows[i].Cells[2].Value);
                            lPair.Lon = Convert.ToDouble(LayersGrid.Rows[i].Cells[1].Value);
    
                            lst = new List<LatLongPair>();
                            lst.Add(lPair);
    
                            LatLongCollectionByName.Add(matchHandle, lst);
    
                            #region KML Placemark Writing
    
                            KMLwriter1.WriteLine("<Placemark>" + "\r\n");
                            KMLwriter1.WriteLine("<name>" + matchHandle + "</name>" + "\r\n");
                            KMLwriter1.WriteLine("<styleUrl>#m_ylw-pushpin</styleUrl>" + "\r\n");
                            KMLwriter1.WriteLine("<Polygon>" + "\r\n");
                            KMLwriter1.WriteLine("<tessellate>1</tessellate>" + "\r\n");
                            KMLwriter1.WriteLine("<outerBoundaryIs>" + "\r\n");
                            KMLwriter1.WriteLine("<LinearRing>" + "\r\n");
                            KMLwriter1.WriteLine("<coordinates>" + "\r\n");
    
    
                         
    
                        }
    
                        foreach (KeyValuePair<string, List<LatLongPair>> item in LatLongCollectionByName)
                        {
                            KMLwriter1.WriteLine(lPair.Lon + "," + lPair.Lat + ",0");
    
                        }
    
                    }
    
    
                    catch
                    {
                        MessageBox.Show("error");
                    }
    
                            #endregion
    
    
                       
                  //  }
    
                    KMLwriter1.WriteLine("\r\n");
                    KMLwriter1.WriteLine("</coordinates>" + "\r\n");
                    KMLwriter1.WriteLine("</LinearRing>" + "\r\n");
                    KMLwriter1.WriteLine("</outerBoundaryIs>" + "\r\n");
                    KMLwriter1.WriteLine("</Polygon>" + "\r\n");
                    KMLwriter1.WriteLine("</Placemark>" + "\r\n");
    
    
                    //end of new code from web
    
                #endregion
    
    
                }
                KMLwriter1.Close();
            }
    thanks in advance

    Jonathan

  • #2
    New Coder
    Join Date
    Aug 2013
    Posts
    37
    Thanks
    1
    Thanked 6 Times in 6 Posts
    Hi jkessous,

    you can figure out where your bug is by using the debugger. http://www.dotnetperls.com/debugging

    There you can see what your program actually does, where it jumps and so on and so forth.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •