M.Efe Ozer’s Weblog

December 13, 2008

Simple Works on GeoRSS with XLinq

Filed under: .Net, Code Stuff, Notes to Myself — Tags: , , — Mehmet Efe Ozer @ 2:54 pm

Note to myself

What is written here is not optimized and not ensure security

This is what i did for my visitor map georss format xml file.

a sample node of my georss:

<?xml version=”1.0″ encoding=”utf-8″?>
<rss xmlns:geo=”http://www.w3.org/2003/01/geo/wgs84_pos#”>
<channel>
<title>Locations</title>
<link></link>
<item>
<title>Hudson, OH UNITED STATES</title>
<city>Hudson</city>
<country>UNITED STATES</country>
<description>10 Visits</description>
<geo:long>-81.4512</geo:long>
<geo:lat>41.2447</geo:lat>
<visitorcount>10</visitorcount>
</item>

</channel>
</rss>

usings:

Imports System.Collections
Imports System.Linq
Imports System.Xml.Linq
'we need it for using :<geo:long></geo:long> <geo:lat></geo:lat> 
Imports <xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#">
'where is my xml file
    Public Shared VisitorDataPathURL As String
'class that store some info
    Public Class UserIpInfo

        Public Property IPAddress() As String...End Property
        Public Property Location() As String...End Property
        Public Property Longitude() As Double...End Property
        Public Property Latitude() As Double...End Property
        Public Property Country() As String...End Property
        Public Property City() As String...End Property
'constructors
        Public Sub New(ByVal iPAddress As String, _
                       ByVal location As String, _
                       ByVal cityname As String, _
                       ByVal countryname As String, _
                       ByVal lon As Double, _
                       ByVal lat As Double
...
        End Sub

        Public Sub New()
        End Sub

    End Class

we provide some info from our application and here are methods :

Public Shared Sub DoVisitorWork(ByVal uinfo As UserIpInfo)
'look for city if already exists
            Dim xdoc As XDocument = XDocument.Load(VisitorDataPathURL)
           Dim searched As XElement = (From xel As XElement In xdoc.Descendants.Elements("item") _
                                  Where xel.Element("city").Value.ToString.Trim.ToLowerInvariant _
                                    = uinfo.City.Trim.ToLowerInvariant _
                                     Select xel).FirstOrDefault
'if city is new , new xelement creating by vb.net xml literal
            If searched Is Nothing Then
                searched = _
                <item>
                    <title><%= uinfo.Location %></title>
                    <description><%= uinfo.Location %></description>
                    <city><%= uinfo.City %></city>
                    <country><%= uinfo.Country %></country>
                    <geo:long><%= uinfo.Longitude %></geo:long>
                    <geo:lat><%= uinfo.Latitude %></geo:lat>
                    <visitorcount>1</visitorcount>
                </item>

‘other methods to create xelement :

                'Dim myxml As String = "<item>" & System.Environment.NewLine & _
                '"<title><%= uinfo.Location %></title>" & System.Environment.NewLine _
        '    & " <description>" & uinfo.Location & "</description>" & System.Environment.NewLine _
                '    & "<city>" & uinfo.City & "</city>" & System.Environment.NewLine _
                '    & "<country>" & uinfo.Country & "</country>" & System.Environment.NewLine _
             '    & "<geo:long>" & uinfo.Longitude & "</geo:long>" & System.Environment.NewLine _
                '    & "<geo:lat>" & uinfo.Latitude & "</geo:lat>" & System.Environment.NewLine _
                '    & "<visitorcount>1</visitorcount></item>"
                'searched = XElement.Parse(myxml)

                'searched = New XElement("item")
                'searched.Add(New XElement("title", uinfo.Location))
                'searched.Add(New XElement("description", uinfo.Location))
                'searched.Add(New XElement("city", uinfo.Location))
                'searched.Add(New XElement("country", uinfo.Location))
                'searched.Add(New XElement("geo:long", uinfo.Longitude))
                'searched.Add(New XElement("geo:lat", uinfo.Latitude))
                'searched.Add(New XElement("visitorcount", 1))

              

‘add our new city

                xdoc.Element("rss").Element("channel").Add(searched)
            Else

‘if city is already exists we add 1 more to visitor count

                searched.Element("visitorcount").Value = _
                (CType(searched.Element("visitorcount").Value.Trim _
                                                          , Int64) + 1).ToString
        searched.Element("description").Value = searched.Element("visitorcount").Value & " Visits"
            End If

’save document

            xdoc.Save(VisitorDataPathURL)
    End Sub

show records on gridview,return value can be diffrent than ilist

  Public Shared Function GetVisitorList() As IList

        Dim MyList = (From ui As XElement _
                      In XDocument.Load(VisitorDataPathURL).Descendants.Elements("item") _
                       Order By ui.Element("country").Value _
                      Select Place = ui.Element("title").Value _
                        , City = ui.Element("city").Value _
                        , Country = ui.Element("country").Value _
                        , Count = CInt(ui.Element("visitorcount").Value)).ToList
'got visitorcount property, will be shown in footer of gridview
        VisitorCount = MyList.Select(Function(count) count.Count).Sum

        Return MyList
    End Function
    Public Shared Property VisitorCount() As Integer...End Property

All this stuff in a class…

In virtual earth show this file with VEDataType.GeoRSS method.  Here is EXAMPLE PAGE

In the future clustured pushpins and rich content pushpin descriptions will be there…

No Comments Yet »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a comment

Blog at WordPress.com.