title: Reading kml file to get coordinates in c#
body: <p>Okay, so I've been working on reading in a kml file that contains the coordinates of the boundary of every county/city in America. However, I've ran into some problems. Particularly, how to get the value of the NextNode and what to do when there is another element tag in the middle of an element's value.</p>
  
  <p>Here is what the kml file looks like:</p>
  
  <pre class="lang-cs prettyprint-override"><code>&lt;?xml version="1.0" encoding="utf-8" ?&gt;
  &lt;kml xmlns="http://www.opengis.net/kml/2.2"&gt;
  &lt;Document id="root_doc"&gt;
  &lt;Schema name="gadm36_USA_2" id="gadm36_USA_2"&gt;
      &lt;SimpleField name="NAME_0" type="string"&gt;&lt;/SimpleField&gt;
      &lt;SimpleField name="NAME_1" type="string"&gt;&lt;/SimpleField&gt;
      &lt;SimpleField name="NAME_2" type="string"&gt;&lt;/SimpleField&gt;
  &lt;/Schema&gt;
  &lt;Folder&gt;&lt;name&gt;gadm36_USA_2&lt;/name&gt;
    &lt;Placemark&gt;
      &lt;Style&gt;&lt;LineStyle&gt;&lt;color&gt;ff0000ff&lt;/color&gt;&lt;/LineStyle&gt;&lt;PolyStyle&gt;&lt;fill&gt;0&lt;/fill&gt;&lt;/PolyStyle&gt;&lt;/Style&gt;
      &lt;ExtendedData&gt;&lt;SchemaData schemaUrl="#gadm36_USA_2"&gt;
          &lt;SimpleData name="NAME_0"&gt;United States&lt;/SimpleData&gt;
          &lt;SimpleData name="NAME_1"&gt;Alabama&lt;/SimpleData&gt;
          &lt;SimpleData name="NAME_2"&gt;Autauga&lt;/SimpleData&gt;
      &lt;/SchemaData&gt;&lt;/ExtendedData&gt;
        &lt;MultiGeometry&gt;&lt;Polygon&gt;&lt;outerBoundaryIs&gt;&lt;LinearRing&gt;&lt;coordinates&gt;-86.8189620971679,32.3402709960939 -86.8108367919922,32.3471298217775 -86.8097915649414,32.3535118103028 -86.8103485107422,32.3585205078126 -86.8158340454101,32.3703498840333 -86.8239974975586,32.3785285949708 -86.8310775756835,32.3839797973634 -86.83544921875,32.3912506103515 -86.8419876098633,32.3980712890626 -86.8452758789062,32.4044418334961 -86.8458633422851,32.4140090942383 -86.8447875976562,32.4167404174805 &lt;/coordinates&gt;&lt;/LinearRing&gt;&lt;/outerBoundaryIs&gt;&lt;/Polygon&gt;&lt;Polygon&gt;&lt;outerBoundaryIs&gt;&lt;LinearRing&gt;&lt;coordinates&gt;-86.8426208496093,32.4181213378907 -86.8361129760742,32.4204101562501 -86.8296127319336,32.4227104187012 -86.8274383544922,32.4240798950195 -86.8263626098633,32.4259109497071 -86.8280029296875,32.4277305603028 -86.8307189941406,32.4295387268066 &lt;/coordinates&gt;&lt;/LinearRing&gt;&lt;/outerBoundaryIs&gt;&lt;/Polygon&gt;&lt;/MultiGeometry&gt;
    &lt;/Placemark&gt;
  </code></pre>
  
  <p>Note, this is not an actual example where these random element tags appear in the middle of the coordinates, the counties that have them typically have a massive coordinate list and from my experience if i go through the kml file and only use the values before the first element tags, it appears to map out the correct county boundaries</p>
  
  <pre class="lang-cs prettyprint-override"><code>List&lt;string&gt; locationList = new List&lt;string&gt;();
  var doc = XDocument.Load("gadm36_USA1.kml");
  XNamespace ns = "http://www.opengis.net/kml/2.2";
  var result = doc.Root.Descendants(ns + "Placemark");
  foreach (XElement xmlInfo in result)
  {
     var region = xmlInfo.Element(ns + "ExtendedData").Element(ns + "SchemaData").Value;
     //var country = region.Element(ns + "SimpleData").Value;
     //var state = region.Element(ns + "SimpleData");
     //var cityCounty = region.Element(ns + "SimpleData");
     locationList = xmlInfo.Element(ns + "MultiGeometry").Value.Split(' ').ToList();
     CountyCoordinates.Add(region, locationList);
  }
  </code></pre>
  
  <p>So when i get to the variable "region", it groups all the element values together. For example it will say "United StatesAutaugaAlabama". </p>
  
  <p>As for the coordinates, since there are these random element tags in the middle of the coordinate values, when i split the coordinates by " ", it gets screwed up when it hits these random element tags. (when it gets to that text in the middle of the coordinates the split will return '-86, 32 -86', instead of just '-86, 32') So, I'm essentially looking for help on how to read in the country, state, and county separately and how to properly read in the coordinates despite these random element tags.</p>