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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T10:58:14+00:00 2026-06-04T10:58:14+00:00

I am working on a project to generate some KML data from our delivery

  • 0

I am working on a project to generate some KML data from our delivery database.

I’m happy with building the KML structure using LINQ but it appears that when the namespace attribute is applied to the node I am then unable to output the data to a string.

This is the code I use to generate the KML:

// Create a new XDocument object
_xDoc = new XDocument(new XDeclaration("1.0", "UTF-8", "yes"));

// Build internal kml document node
XElement document = CreateKmlDocumentNode();

if (document != null)
{
    // Add data points to the kml document node
    foreach (KmlData delivery in _deliveryData)
    {
        document.Add( CreatePlacemark(delivery) );
    }
}

// Add the document node to the kml node
XElement kml = new XElement("kml",
                            document);

// ** Comment out this line and the output is generated **
kml.Add( new XAttribute("xmlns", @"http://earth.google.com/kml/2.2"));

// And finally add the kml node to the XDocument
_xDoc.Add( kml );

This is the code I use to produce the string:

string output;

using (var stringWriter = new StringWriter())
{
    XmlWriterSettings xws = new XmlWriterSettings();
    xws.NamespaceHandling = NamespaceHandling.OmitDuplicates;
    xws.Indent = true;

    using (var xmlTextWriter = XmlWriter.Create(stringWriter, xws))
    {
        // The Line below throws the exception when the namespace attribute is added
        _xDoc.WriteTo(xmlTextWriter);

        xmlTextWriter.Flush();
        output = stringWriter.GetStringBuilder().ToString();
    }
}
return output;

The exception text that is generated:

{“The prefix ” cannot be redefined from ” to ‘http://earth.google.com/kml/2.2’ within the same start element tag.”}

Here is a sample of what I want the data to look like:

<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://earth.google.com/kml/2.2">
  <Document>
    <name>Sample</name>
    <description><![CDATA[]]></description>

    <Style id="depot">
      <IconStyle>
        <Icon>
          <href>http://maps.gstatic.com/mapfiles/ms2/micons/rangerstation.png</href>
        </Icon>
      </IconStyle>
    </Style>
    <Style id="pickupIcon">
      <IconStyle>
        <Icon>
          <href>http://maps.gstatic.com/mapfiles/ms2/micons/truck.png</href>
        </Icon>
      </IconStyle>
    </Style>
    <Style id="letterIcon">
      <IconStyle>
        <Icon>
          <href>http://maps.gstatic.com/mapfiles/ms2/micons/postoffice-us.png</href>
        </Icon>
      </IconStyle>
    </Style>
    <Placemark>
      <name>Chester Depot</name>
      <description><![CDATA[]]></description>
      <styleUrl>#depot</styleUrl>
      <Point>
        <coordinates>-2.881701,53.197021,0.000000</coordinates>
      </Point>
    </Placemark>
    <Placemark>
      <name>15 Hankelow Close</name>
      <description><![CDATA[<div><b><font size="4">Delivery Successful - 10:14am</font></b></div><div style="font-size:10pt"><b>Contact </b>Sam Spade</div><b style="font-size:10pt">Address </b><font size="2">15 Hankelow Close, Chester, Cheshire West and Chester CH2 2DZ, UK]]></description>
      <styleUrl>#letterIcon</styleUrl>
      <Point>
        <coordinates>-2.889466,53.199226,0.000000</coordinates>
      </Point>
    </Placemark>
    <Placemark>
      <name>45 Victoria Rd</name>
      <description><![CDATA[<div><b><font size="4">Pickup Successful - 1:24pm</font></b></div><div style="font-size:10pt"><b>Contact </b>Sam Spade</div><b style="font-size:10pt">Address </b><font size="2">Chester, Cheshire West and Chester CH2 2AX, UK]]></description>
      <styleUrl>#pickupIcon</styleUrl>
      <Point>
        <coordinates>-2.892855,53.198498,0.000000</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-04T10:58:15+00:00Added an answer on June 4, 2026 at 10:58 am

    I resolved this issue by using SharpKML library rather than constructing the KML from scratch.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using T4 templating to generate some .config files in a project I'm working
I'm curently working on a project where I need to transfer data (with some
I'm currently working on a project which uses XSL-Transformations to generate HTML from XML.
I'm working on a project that will generate an OAuth querystring in JavaScript, using
I am working to generate terrain for our project, something that will be contained
I'm working on a project that generates a iOS static library, and some sample
I am working on a project in which I need to generate 8 random
I am currently working on a project where I need to generate multiple values
I am working on a homework project in which we need to generate a
While working a project tonight, I ended up using one .js resource file for

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.