Click here to Skip to main content
11,923,046 members (65,257 online)
Rate this:
Please Sign up or sign in to vote.
See more: C# XML CSV KML
Is there a way to convert/parse a KML file's coordinates into a format (CSV or XML) that I could later use to import into other programs.

I have polygons in KML files and I'm trying to reuse the coordinates with other programs in different formats.

In my KML file I have coordinates that look like this.

-86.36762,37.31916,0 -86.43890,37.31916,0 -88.96934,32.11572,0 -84.51434,32.68596,0 -82.87490,35.85792,0 -86.36762,37.31916,0

How do I search the KML file and convert the coordinates to a format like this


and save it in a CSV using C#?

Any help would be much appreciated. I'm able to find ways to convert CSV to KML no problem but what about the other way around.

Posted 31-May-12 2:54am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

If the requirement is to break the set of coordinates into separate lines, then the Replace method of Regex class can be used to replace the spaces which are not preceded by or followed by a comma as shown below:
string kmlInput = @"-86.36762, 37.31916 , 0 -86.43890,37.31916,0 -88.96934,32.11572,0 -84.51434,32.68596,0 -82.87490,35.85792,0 -86.36762,37.31916,0 ";
    string csvOutput = Regex.Replace(kmlInput,
    Console.WriteLine (csvOutput);
//-86.36762, 37.31916 , 0
mzrax 31-May-12 9:19am
VJ Reddy


I've taken what you've done to look like this.

string filePath = @"C:\test.csv";
string kmlInput = @"-86.36762, 37.31916 , 0 -86.43890,37.31916,0 -88.96934,32.11572,0 -84.51434,32.68596,0 -82.87490,35.85792,0 -86.36762,37.31916,0 ";
string csvOutput = Regex.Replace(kmlInput,
@"(?<=[^,]+)\s+(?=[^,]+)", "\n",
System.IO.StreamWriter objWriter;
objWriter = new StreamWriter(filePath, true);

This is great. I really appreciate it.

Thanks Again.
VJ Reddy 31-May-12 10:36am
You're welcome and thank you for accepting the solution :)
The kmlInput can be read from the file using
string kmlInput = System.IO.File.ReadAllText(filePath);
provided the file contains the required input

csvOutput can be written to file using
System.IO.File.WriteAllText(outFilePath, csvOutput);
umarabbas 14-Nov-12 6:36am
losmac 31-May-12 9:28am
Short and to the point, my 5!
VJ Reddy 31-May-12 10:37am
Thank you, losmac :)
Björn Ranft 31-May-12 15:36pm
Correct, my 5!Regards
VJ Reddy 31-May-12 19:49pm
Thank you, Björn :)
Espen Harlinn 1-Jun-12 4:16am
Well answered :-D
VJ Reddy 1-Jun-12 5:50am
Thank you, Espen :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web01 | 2.8.151125.3 | Last Updated 31 May 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100