友情提示,您阅读本篇博文的先决条件如下:
1、本文示例基于Microsoft SQL Server 2008 R2调测。
2、具备 Transact-SQL 编程经验和使用 SQL Server Management Studio 的经验。
3、熟悉或了解Microsoft SQL Server 2008中的空间数据类型。
4、具备相应(比如OGC规范、KML规范)的GIS专业理论知识。
5、GeoRss订阅技术以及其他相关知识。
using System.Collections.Generic;
using Microsoft.Maps.MapControl;
namespace GeoRss.Map.GeoRssUtils
{
public class GeoRssItem
{
public string Title { get; set; }
public string Description { get; set; }
public string Link { get; set; }
public string PubData { get; set; }
public LocationCollection Locatios { get; set; }
}
}
核心原理就是使用WebClient动态的发起http请求,将返回的GeoRss数据通过Linq To XML的方式解析为对象结构的数据。其实现非常简单,不做具体分析,详细代码如下所示:
using System.Collections.Generic;
using System;
using System.Net;
using System.Xml.Linq;
using System.Linq;
using System.Windows;
using Microsoft.Maps.MapControl;
namespace GeoRss.Map.GeoRssUtils
{
public delegate void DownloadGeoRssCompletedEventHandler(List items);
public delegate void DownloadGeoRssExceptionEventHandler(Exception e);
public class GeoRssReader
{
public GeoRssReader()
{
wc = new WebClient();
wc.DownloadStringCompleted += WebClientDownloadGeoRssCompleted;
}
public GeoRssReader(Uri uri)
: this()
{
this.uri = uri;
}
public GeoRssReader(Uri uri, DownloadGeoRssCompletedEventHandler evh)
: this(uri)
{
DownloadGeoRssCompleted += evh;
}
public Uri uri { get; set; }
public event DownloadGeoRssCompletedEventHandler DownloadGeoRssCompleted;
public event DownloadGeoRssExceptionEventHandler DownloadGeoRssException;
public void ReadAsync()
{
if (DownloadGeoRssCompleted.Target != null)
{
wc.DownloadStringAsync(uri);
}
}
#region _private
private readonly WebClient wc;
private void WebClientDownloadGeoRssCompleted(object sender, DownloadStringCompletedEventArgs e)
{
try
{
XNamespace nsXml = "http://www.w3.org/2005/Atom";
XNamespace nsGeorss = "http://www.georss.org/georss";
XNamespace nsGeo = "http://www.w3.org/2003/01/geo/wgs84_pos#";
XNamespace nsMedia = "http://search.yahoo.com/mrss/";
var items = from item in XElement.Parse(e.Result).Descendants("item")
select new GeoRssItem
{
Title = (item.Element("title") != null) ? item.Element("title").Value : null,
Link = (item.Element("link") != null) ? item.Element("link").Value : null,
Description = (item.Element("description") != null) ? item.Element("description").Value : null,
PubData = (item.Element("pubDate") != null) ? item.Element("pubDate").Value : null,
Locatios = ParserLocations(XElement.Parse(item.LastNode.ToString().Replace(":", "X")).Value)
};