Click here to Skip to main content
11,812,712 members (72,360 online)
Rate this: bad
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 1: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 at 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 at 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 at 14-Nov-12 6:36am
losmac at 31-May-12 9:28am
Short and to the point, my 5!
VJ Reddy at 31-May-12 10:37am
Thank you, losmac :)
Björn Ranft at 31-May-12 15:36pm
Correct, my 5!Regards
VJ Reddy at 31-May-12 19:49pm
Thank you, Björn :)
Espen Harlinn at 1-Jun-12 4:16am
Well answered :-D
VJ Reddy at 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
0 KrunalRohit 184
1 CHill60 180
2 Abhinav S 175
3 Palash Mondal_ 160
4 RyanDev 130
0 OriginalGriff 3,893
1 KrunalRohit 2,441
2 CPallini 2,240
3 Maciej Los 2,155
4 ppolymorphe 1,725

Advertise | Privacy | Mobile
Web03 | 2.8.151002.1 | 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