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 8653713
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T14:36:35+00:00 2026-06-12T14:36:35+00:00

Possible Duplicate: How to set the default XML namespace for an XDocument I am

  • 0

Possible Duplicate:
How to set the default XML namespace for an XDocument

I am trying to write a piece of code in Asp.net C#, in order to create a KML file on-the-fly and store it in a specific path.
The code gives an error when i want to add the xmlns=”http://earth.google.com/kml/2.2″ attribute of kml tag (see below). I tried replacing xmlns with another word like “id” and it works just fine. Does it have something to do with the word “xmlns” ??! pretty strange for me.

Please provide me a solution if you understand what the problem is… Thanks!

My code:

XDocument doc = new XDocument(
        new XDeclaration("1.0", "utf-8", ""),
        new XComment("This is comment by me"),
        new XElement("kml", new XAttribute("xmlns", "http://earth.google.com/kml/2.2"),
        new XElement("Document",
        new XElement("Name", "something"), new XElement("Placemark",
        new XAttribute("id", "1"),
        new XElement("title", "something"),
        new XElement("description", "something"),
        new XElement("LookAt",
        new XElement("Longitude", "49.69"),
        new XElement("Latitude", "32.345")), new XElement("Point", new XElement("Coordinates", "49.69,32.345,0"))))));
        doc.Save(Server.MapPath(@"~\App_Data\markers.xml"));

The runtime error it gives:

The prefix ” cannot be redefined from ” to
‘http://earth.google.com/kml/2.2‘ within the same start element tag.
Description: An unhandled exception occurred during the execution of
the current web request. Please review the stack trace for more
information about the error and where it originated in the code.

Exception Details: System.Xml.XmlException: The prefix ” cannot be
redefined from ” to ‘http://earth.google.com/kml/2.2‘ within the same
start element tag.

The kml file i would LIKE to create:

<?xml version="1.0" encoding="utf-8"?>
<!--This is comment by me-->
<kml xmlns="http://earth.google.com/kml/2.2">
  <Document>
    <Name>something</Name>
    <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>
  </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-12T14:36:36+00:00Added an answer on June 12, 2026 at 2:36 pm

    Define the namespace first

    XNamespace n = "http://earth.google.com/kml/2.2";
    
    new XElement(n+"kml")//just do n+ for each underlying elements
    

    Also your XML structure is wrong,it should be like

       XNamespace n = "http://earth.google.com/kml/2.2";
    XDocument doc = new XDocument(
    new XDeclaration("1.0", "utf-8", ""),
    new XComment("This is comment by me"),
    new XElement(n+"kml",
     new XElement(n+"Document",
            new XElement(n+"Name", "something"), new XElement(n+"Placemark",
            new XAttribute("id", "1"),
            new XElement(n+"title", "something"),
            new XElement(n+"description", "something"),
            new XElement(n+"LookAt",
            new XElement(n+"Longitude", "49.69"),
            new XElement(n+"Latitude", "32.345")), new XElement(n+"Point", new XElement(n+"Coordinates", "49.69,32.345,0")))))
    
    );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: ASP.Net: DropDownList's SelectedIndexChanged event not firing I created a new ASP.NET default
Possible Duplicate: Set Locale programatically I would like to create an english/spanish option choice
Possible Duplicate: Android Button: set onClick background image change with XML? I need that
Possible Duplicate: Error on calling default constructor with empty set of brackets When I
Possible Duplicate: Default class inheritance access I know I can set the protection level
Possible Duplicate: Why write <script type=“text/javascript”> when the mime type is set by the
Possible Duplicate: How do you set a default value for a MySQL Datetime column?
Possible Duplicate: where do you put ivysettings.xml? I have set up Artifactory to be
Possible Duplicate: How do I set environment variables from Java? I'm trying to set
Possible Duplicate: Link Button on the page and set it as default button, work

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.