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 .Xml ;
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:
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
- Browser Query www.maps.google.com/?q=from "from_address" to "to_address" "&output=kml&view=text"; (where "from" and "to" are the keywords)
- The "output" parameter retrieves the result in the given format which will be KML in this case.
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.
from n in Des_Source_document.Descendants(obj_XNameSpace + "Placemark")select n;"
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
foreach loop. After it's done adding the rows, I assign the
DataSource and then bind the
- 28th January, 2010: Initial post