none
Writing KML in C# .NET RRS feed

  • Question

  • Hi,

    I apologise if this is a little off topic, but I thought I might get a better response here than on the XML forums.

    I'm trying to write KML from my .NET application.  I used the command line to create a class that represents KML:

    xsd.exe /classes

    I'm now trying to use these classes to create a KML document, as a bunch of objects.  I was then going to use the .NET serialize tools to write the kml from .NET.  It's proving harder than I had hoped, putting the classes together to create a document.

    Has anyone else tried writing KML this way?  Am I on the wrong track?

    Rob
    • Moved by Ricky_Brundritt Friday, March 9, 2012 4:34 PM (From:Bing Maps: Map Control and Web services Development)
    Monday, January 21, 2008 8:55 PM

Answers

  • Hi

    When I have done this I have created the xml, either directly in the DOM or by loading template xml and inserting the actual values using the DOM. I create a simple collection of clases to represent the bits of kml I need and each loads its kml template from a file (cached in application memory so it does not need to be loaded each time).

    All I then do is create one large KML file then use response.binarywrite to write it out.

    A few tips to help you along:

    1) I found to get browsers to correctly identify the kml when downloading you need to do the following:

    Response.ClearHeaders();
    Response.ContentType = "application/vnd.google-earth.kmz kmz;";   //this line will depend on if you want kml or kmz to be written

    2) Use kmz, using SharpZipLib to compress you KMZ document:

            Dim kmz() As Byte

            Dim ms As New System.IO.MemoryStream
            Dim zs As ZipOutputStream = New ZipOutputStream(ms)
            zs.SetLevel(6)
            zs.IsStreamOwner = True

            Dim buffer As Byte()
            'read kml into buffer
            buffer = System.Text.Encoding.UTF8.GetBytes(YOUR KML AS STRING)

            'create zipped kml entry
            Dim zipEntry As New ZipEntry("Doc.kml")
            With zipEntry
                .DateTime = DateTime.Now()
                .Size = buffer.Length
            End With

            zs.PutNextEntry(zipEntry)
            zs.Write(buffer, 0, buffer.Length)
            zs.CloseEntry()

            zs.Finish()
            zs.Close()
            ms.Close()

            kmz = ms.ToArray()

    3) If you data does not change often, or even if it does but only hourly, cache it. Either using asp.net caching or the response caching for downlevel browser and proxy caching

    Hope that helps, let me know
    Brian Norman
    www.earthware.co.uk


    Monday, January 21, 2008 9:33 PM
    Moderator
  • I had a similar need for a program that worked with KML. The program I wrote converted KML to geoRSS. Not exactly what your looking for but it is written in C# and it would be a good place to start to see how to put together the XML. The program can be found here: www.geocities.com/saintsairforce/KMLtoGeoRSSTranslator.zip

    Thursday, January 31, 2008 2:42 PM

All replies

  • Hi

    When I have done this I have created the xml, either directly in the DOM or by loading template xml and inserting the actual values using the DOM. I create a simple collection of clases to represent the bits of kml I need and each loads its kml template from a file (cached in application memory so it does not need to be loaded each time).

    All I then do is create one large KML file then use response.binarywrite to write it out.

    A few tips to help you along:

    1) I found to get browsers to correctly identify the kml when downloading you need to do the following:

    Response.ClearHeaders();
    Response.ContentType = "application/vnd.google-earth.kmz kmz;";   //this line will depend on if you want kml or kmz to be written

    2) Use kmz, using SharpZipLib to compress you KMZ document:

            Dim kmz() As Byte

            Dim ms As New System.IO.MemoryStream
            Dim zs As ZipOutputStream = New ZipOutputStream(ms)
            zs.SetLevel(6)
            zs.IsStreamOwner = True

            Dim buffer As Byte()
            'read kml into buffer
            buffer = System.Text.Encoding.UTF8.GetBytes(YOUR KML AS STRING)

            'create zipped kml entry
            Dim zipEntry As New ZipEntry("Doc.kml")
            With zipEntry
                .DateTime = DateTime.Now()
                .Size = buffer.Length
            End With

            zs.PutNextEntry(zipEntry)
            zs.Write(buffer, 0, buffer.Length)
            zs.CloseEntry()

            zs.Finish()
            zs.Close()
            ms.Close()

            kmz = ms.ToArray()

    3) If you data does not change often, or even if it does but only hourly, cache it. Either using asp.net caching or the response caching for downlevel browser and proxy caching

    Hope that helps, let me know
    Brian Norman
    www.earthware.co.uk


    Monday, January 21, 2008 9:33 PM
    Moderator
  • Hi Brian,

    Thanks for responding - Interesting to see you've used Sharp!  I'm actually trying to do this with a desktop c# application, sorry I know that isn't obvious posting on the VE forum, I just thought the desktop XML guys wouldn't know that much about KML.  But in my gut, I think I will have to take a similar approach to you, and cook my own classes or write the KML from Scratch.  I was just hoping someone might have used the xsd.exe with KML and could peice the classes together that it spits out?

    Thank You -

    Rob
    Monday, January 21, 2008 11:50 PM
  •  

    Hi Rob

     

    I had the same need so I developed a "KML Builder" assembly in C# that have the KML objects with properties  - and when serialized, build correct KML.

     

    I'm at a point where it is stable and the idea was top put it in the public domain. It would be nice if i can share it with you and you QA it or just give some feedback?

     

    What is outstanding is to have XSD validation available - that i would like, currently i open the KML in GE to validate results every time.

     

    Regards, Gert

    Thursday, January 31, 2008 2:27 PM
  • I had a similar need for a program that worked with KML. The program I wrote converted KML to geoRSS. Not exactly what your looking for but it is written in C# and it would be a good place to start to see how to put together the XML. The program can be found here: www.geocities.com/saintsairforce/KMLtoGeoRSSTranslator.zip

    Thursday, January 31, 2008 2:42 PM
  • Hi Gert,

    I would be more than happy to QA, if you could share the code that would be great.  I was really surprised to find this wasn't already public domain.  I'm also a little surprised Google have done more to help others publish KML?

    Anyhow, if I could take a peak at what you have, that would be great, and I'll happyily provide feedack.

    Regards,

    Rob - robsroom at hotmail dot com
    Friday, February 1, 2008 10:44 AM
  • Thanks Rob

     

    I send the source code.

     

    Gert

     

    Friday, February 1, 2008 11:51 AM
  • Gert,

    when it is ready you should post about it here.

    If you want to write a quick or lengthy article on it it would make a great addition at http://www.viawindowslive.com/VirtualEarth.aspx

     

    John.

     

    Thursday, February 7, 2008 8:16 AM
    Moderator
  • Hi Gert,

    I also want to use your software.

    my email is liuliangchina@gmail.com
    Sunday, April 27, 2008 2:15 AM
  • Google actually has. There is libkml - http://code.google.com/p/libkml/.

     

    No one has 'tweaked' the SWIG configuration to make c# bindings yet. 

    Monday, April 28, 2008 8:54 PM
  • Has anyone also checked out: http://code.google.com/p/kml-library/  ??

    This would be really handy!!
    -Pure Krome-
    Thursday, May 7, 2009 1:24 PM
  • Hi Richard,

    The link is not accessible any more. Would you mind sharing this again?

    Thanks,

    Josselin

     

    Friday, November 5, 2010 2:37 PM
  • This is a pretty old post. I have the code available here: http://cid-e7dba9a4bfd458c5.office.live.com/self.aspx/VE%20Tools/KML%20to%20GeoRSS%20Translator.zip But it's very old code and there are much better ways to do things now.
    Windows Live Developer MVP - http://rbrundritt.spaces.live.com | http://inknowledge.co.uk
    Friday, November 5, 2010 5:24 PM
  • Hi Richard,

    Can you provide guidance on the "much better ways to do things now"?  In particular, I need to implement KMZ input/output for a silverlight application running on Azure.

    Thanks.

    Tuesday, March 15, 2011 6:41 PM
  • also I recently noticed the sql spatial team have added a kml parser to their codeplex project, worth a look:

     

    http://sqlspatialtools.codeplex.com/SourceControl/changeset/view/61525#


    Brian @ Earthware - UK interactive mapping web developers http://www.earthware.co.uk/blog | http://www.twitter.com/earthware | Windows Live Developer MVP
    Wednesday, March 16, 2011 10:46 AM
    Moderator