title: generating KML file using C# and LINQ with a recursive function
body: <p>I am trying to create a KML file dynamically in C#. I have wrote a recursive function to do this. However the result of output has a bit problem. The problem is the position of closing tags of all placemarks. I am really confused. Please tell me where am I making mistake in the recursive function???</p>
  
  <p>My code:</p>
  
  <pre><code>private void xmlBuild()
      {
  XDocument doc = new XDocument(
          new XDeclaration("1.0", "utf-8", ""),
          new XComment("This is comment by me"),
          new XElement(ns+"kml", 
          new XElement(ns+"Document", rec_build())));      
          doc.Save(Server.MapPath(@"~\App_Data\markers2.xml"));
  }
  private XElement rec_build()
      {
          if (iteration != 0)
          {
              iteration -= 1;
              return final_rec = new XElement(ns + "Placemark",
                      new XAttribute("id", "1"),
                      new XElement(ns + "title", "something"),
                      new XElement(ns + "description", "something"),
                      new XElement(ns + "LookAt",
                      new XElement(ns + "Longitude", "49.69"),
                      new XElement(ns + "Latitude", "32.345")), new XElement(ns + "Point", new XElement(ns + "coordinates", "49.69,32.345,0")),rec_build());
          }
          else
          {
              return null;
          }
      }
  </code></pre>
  
  <p>and this is the output for iteration value of 2: (please notice the closing tags of placemark id=1 at the end of file. It should be before the starting tag of placemark id=2!</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;!--This is comment by me--&gt;
  &lt;kml xmlns="http://earth.google.com/kml/2.2"&gt;
    &lt;Document&gt;
      &lt;Placemark id="1"&gt;
        &lt;title&gt;something&lt;/title&gt;
        &lt;description&gt;something&lt;/description&gt;
        &lt;LookAt&gt;
          &lt;Longitude&gt;49.69&lt;/Longitude&gt;
          &lt;Latitude&gt;32.345&lt;/Latitude&gt;
        &lt;/LookAt&gt;
        &lt;Point&gt;
          &lt;coordinates&gt;49.69,32.345,0&lt;/coordinates&gt;
        &lt;/Point&gt;
        &lt;Placemark id="1"&gt;
          &lt;title&gt;something&lt;/title&gt;
          &lt;description&gt;something&lt;/description&gt;
          &lt;LookAt&gt;
            &lt;Longitude&gt;49.69&lt;/Longitude&gt;
            &lt;Latitude&gt;32.345&lt;/Latitude&gt;
          &lt;/LookAt&gt;
          &lt;Point&gt;
            &lt;coordinates&gt;49.69,32.345,0&lt;/coordinates&gt;
          &lt;/Point&gt;
        &lt;/Placemark&gt;
      &lt;/Placemark&gt;
    &lt;/Document&gt;
  &lt;/kml&gt;
  </code></pre>