Click here to Skip to main content
13,831,897 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


12 bookmarked
Posted 28 Jan 2010
Licenced CPOL

Transformation of KML into ASP.NET

, 28 Jan 2010
Rate this:
Please Sign up or sign in to vote.
An article on how to integrate the Keyhole Markup Language KML document in ASP.NET using C#


Figure 1


In this example, It has been demonstrated how you can integrate the Keyhole Markup Language KML document in ASP.NET using C Sharp. The Google maps return this document as a result of HTTP request sent by the user which contains the data for direction and distance. You will see the implementation and translation when you walk into the code. I have used LINQ that allows XML data to be queried by using the standard query operators as well as tree-specific operators that provide XPath-like navigation through descendants, ancestors, and siblings. It simplifies working with XML data without having to resort to using additional language syntax like XPath or XQuery. You can use LINQ to XML to perform LINQ queries over XML that you retrieve from the file system, from a remote web service, or from an in-memory XML content.

Using the Code

The first thing we will do is add the namespaces we will be using. Our code-behind will look something like this:

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using System .Xml ;


Figure 2
Line 1:	XNamespace obj_XNameSpace = XNamespace.Get("");
Line 2:	string Des_Source_URL = " " + 
         from_text + " to " + 
	to_text + "&output=kml&view=text";

Before going into detail, there are some points to keep in mind:

  1. XNamespace ("") this will works as a namespace which we will use to parse the KML for the specified Uniform Resource Identifier
  2. Browser Query "from_address" to "to_address" "&output=kml&view=text"; (where "from" and "to" are the keywords)
  3. The "output" parameter retrieves the result in the given format which will be KML in this case.


Figure 3

Hint: If you type the Query above in step 2 in the browser URL window after a filedownload dialog will appear and ask you to save the file.

The code is really simple and straight forward. If you are familiar with LINQ, then I bet you can do more inquisitive things. Right now it's just a basic example where I have created a user control which mainly consists of two Text boxes, Button and a Grid view. I am using the ScriptManager for AJAX functionality in order to improve the performance and less postback.

In the following LINQ query, I am loading all the descendant elements of "Placemark".in IEnumerable<Xelement> object as shown in Figure 2. And I need to get just Placemark(name,description) WITHOUT StyleMap, Point, LookAt (PlaceMark) I'm using the following code, but due to the fact that all nodes are "Node", I get all levels of Descendants. I would like to limit it to only one level.

"IEnumerable<XElement>nodes = 
    from n in Des_Source_document.Descendants(obj_XNameSpace + "Placemark")select n;"   

The Xdocument.Descendants() returns a collection of the descendant elements for this document or element, in document order.

var Select_Name_Doc = from n in nodes where ((from x in n.Descendants(obj_XNameSpace
    + "name") select x).Count() > 0) && ((from y in n.Descendants(obj_XNameSpace
    + "description") select y).Count() > 0) select n;

Inside this LINQ query, I am getting the value of required nodes value. Which are "name" and "description". Please make sure that XML is case sensitive so the Xname should be precisely in the same format as shown in Figure 2.

foreach (var node in Select_Name_Doc)
              DataRow dr = obj_datatble.NewRow();
              dr["Value"] = node.Descendants(obj_XNameSpace + "name").First().Value;
              dr["Distance"] = node.Descendants(obj_XNameSpace +

          GridView1.DataSource = obj_datatble;


After getting all the values in Select_Name_Doc, I declare a custom DataTable with two rows "value" and "distance" and then store the value of each node in these rows and then add in to a DataTable inside foreach loop. After it's done adding the rows, I assign the DataTable to GridView as DataSource and then bind the datagrid.


  • 28th January, 2010: Initial post


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Web Developer
United States United States
I am an Application Developer and currently working for the title company .

You may also be interested in...

Comments and Discussions

Generalyour project is not downloadable Pin
isaiyavan babu karan29-Jan-10 2:01
memberisaiyavan babu karan29-Jan-10 2:01 
GeneralRe: your project is not downloadable Pin
imranahmed.syed29-Jan-10 6:12
memberimranahmed.syed29-Jan-10 6:12 
GeneralRe: your project is not downloadable Pin
Guler Dan Remus29-Dec-12 15:34
memberGuler Dan Remus29-Dec-12 15:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web06 | 2.8.190114.1 | Last Updated 28 Jan 2010
Article Copyright 2010 by imranahmed.syed
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid