1

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>
TeamWild
  • 2,460
  • 8
  • 43
  • 53

1 Answers1

1

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

TeamWild
  • 2,460
  • 8
  • 43
  • 53