Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8687905
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T23:13:51+00:00 2026-06-12T23:13:51+00:00

I am trying to create a KML file dynamically in C#. I have wrote

  • 0

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???

My 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;
        }
    }

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!

<?xml version="1.0" encoding="utf-8"?>
<!--This is comment by me-->
<kml xmlns="http://earth.google.com/kml/2.2">
  <Document>
    <Placemark id="1">
      <title>something</title>
      <description>something</description>
      <LookAt>
        <Longitude>49.69</Longitude>
        <Latitude>32.345</Latitude>
      </LookAt>
      <Point>
        <coordinates>49.69,32.345,0</coordinates>
      </Point>
      <Placemark id="1">
        <title>something</title>
        <description>something</description>
        <LookAt>
          <Longitude>49.69</Longitude>
          <Latitude>32.345</Latitude>
        </LookAt>
        <Point>
          <coordinates>49.69,32.345,0</coordinates>
        </Point>
      </Placemark>
    </Placemark>
  </Document>
</kml>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T23:13:52+00:00Added an answer on June 12, 2026 at 11:13 pm

    So the problem is each time you recurse, you are adding the element to the newly created item. It seems that a loop would work better.

    Essentially code is doing this:

    set up the kml outbody
    1st call and add element (element 1) to kml outerboy
    2nd call add element (element 2) to (element 1)
    3rd call add element (element 3) to (element 2).

    If you wanted to do a recursive method rather then the looping mechanism, pass in a reference to the outer kml .

    The recursive is more confusing if this is exactly how it works

    (Sorry if I have an extra or missing parenthesis, comma, or other item. I don’t have VS installed on this)

    Looping:

    private void xmlBuild()
    {
        XElement documentElement = new XElement(ns + "Document");
    
        for (int i = 0; i < 2; i++)
        {
            documentElement.Add(rec_build());
        }
    
        XDocument doc = new XDocument(
            new XDeclaration("1.0", "utf-8", ""),
            new XComment("This is comment by me"),
            new XElement(ns + "kml", documentElement));
    
        doc.Save(Server.MapPath(@"~\App_Data\markers2.xml"));
    }
    
    private XElement rec_build()
    {
    
        return 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")));
    }
    

    Recursive:

    private void xmlBuild()
    {
        XElement docElement = new XElement(ns+"Document");
        rec_build(docElement);
        XDocument doc = new XDocument(
        new XDeclaration("1.0", "utf-8", ""),
        new XComment("This is comment by me"),
        new XElement(ns+"kml", docElement)));      
        doc.Save(Server.MapPath(@"~\App_Data\markers2.xml"));
    }
    private XElement rec_build(XElement doc)
    {
        if (iteration != 0)
        {
            iteration -= 1;
            doc.Add(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")));
            return recBuild(doc);
        }
        else
        {
            return null;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to use my URLconf file (url.py) to create a KML file of
I'm trying create this function such that if any key besides any of the
I'm trying to create an overlay from a file I have hosted on a
I have this code, I'm trying to toggle some kml layers. The problem is
I'm getting a deadlineexceedederror when trying to create a kml file by a cron
Trying to create a background-image slideshow and am getting this error... This is the
As the title suggest i have (sometimes) a concurrentmodificationexception while trying to create a
I'm trying create a log specifically for logging user account activity. I created this
I'm trying create an immutable object and initialise it from xml config file in
I'm trying create a query that will output a total number, as well as

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.