Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Transformation of KML into ASP.NET

0.00/5 (No votes)
28 Jan 2010 1  
An article on how to integrate the Keyhole Markup Language KML document in ASP.NET using C#

GoogleMapDirection

Figure 1

Introduction

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 ;

kmldocstructure.JPG

Figure 2
Line 1:	XNamespace obj_XNameSpace = XNamespace.Get("http://earth.google.com/kml/2.0");
Line 2:	string Des_Source_URL = "http://maps.google.com/?q=From " + 
         from_text + " to " + 
	to_text + "&output=kml&view=text";

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

  1. XNamespace ("http://earth.google.com/kml/2.0") this will works as a namespace which we will use to parse the KML for the specified Uniform Resource Identifier
  2. Browser Query www.maps.google.com/?q=from "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.

FileDownloadpic.JPG

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 + 
					"description").First().Value;
                obj_datatble.Rows.Add(dr);
            }

            GridView1.DataSource = obj_datatble;

            GridView1.DataBind();

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.

History

  • 28th January, 2010: Initial post

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here