Consuming BlogEngine.net RSS

by Idaho Web Designer 29. July 2009 19:52

I have been trying to display my most recent posts on the home page of onecubicleover.com.  I was trying to consume the RSS provided by BlogEngine.Net using ASP.NET and C#.  When I used the code:

DataSet ds = new DataSet();
string blogURL = "http://www.onecubicleover.com/blog/syndication.axd";
XmlTextReader rdr = new XmlTextReader(blogURL);
ds.ReadXml(rdr);
datagrid.DataSource = ds.Tables[2];
datagrid.DataBind();

I get the error "A column named 'title' already belongs to this DataTable" when it reaches the ds.ReadXml(rdr) line.  The problem lies in the syndication.axd file.  The tag <title> is used under <channel> as well as under <item>.  A quick XSLT file and <asp:Xml/> control instead of a datagrid on the default.aspx page was the solution for me.  Here's what I used:

On the default.aspx page I placed:

<asp:Xml runat="server" ID="xml1" TransformSource="XSLTFile.xslt" /> 

On the default.aspx.cs page I used

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using System.Xml.Xsl;
using System.Data;

public partial class _default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //Show the latest blog post snippet
        string blogURL = "http://www.onecubicleover.com/blog/syndication.axd";

        XmlDocument rssRead = new XmlDocument();
        rssRead.Load(blogURL);
        xml1.Document = rssRead;

    }
}

And finally my XSLTFile.xslt file:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:template match="/">
    <table>
      <xsl:for-each select="rss/channel/item">
        <tr>
          <td>
            <xsl:element name="a">
              <xsl:attribute name="href">
                <xsl:value-of select="link"/>
              </xsl:attribute>
              <xsl:value-of select="title"/>
            </xsl:element>
          </td>
        </tr>
      </xsl:for-each>
    </table>
  </xsl:template>
</xsl:stylesheet>

Tags: , , ,

ASP.NET | CSS | RSS

Displaying Dynamic KML with ASP.NET

by Idaho Web Designer 22. June 2009 20:06

Backcountry Secrets wanted to display KML files for each of their points on their maps.  I didn't want to store all of the KML files on my server, but I wanted to store them in a MySQL database.  The first step was to get the KML file uploaded to the server.  Here's my code I used:

protected void cmdUpload_Click(object sender, EventArgs e)
    {
        MySqlConnection conn = new MySqlConnection(ConfigurationManager.ConnectionStrings["MySQLConnectionString"].ToString());
        MySqlCommand cmd = conn.CreateCommand();

        Stream imageStream = uploadKMLFile.PostedFile.InputStream;
        TextReader t = new StreamReader(imageStream);

        string kmlText = t.ReadToEnd();

        //Insert KML string to the database
        cmd.CommandText = "INSERT INTO kmlfilestable (kmlstring) " +
                            "VALUES (?KML)";
        conn.Open();

        cmd.Parameters.Add("?KML", MySqlDbType.Text);
        cmd.Parameters["?KML"].Value = kmlText;

        cmd.ExecuteNonQuery();
        conn.Close();

        lblUploadSuccess.Text = "Thank you for uploading this KML. We will now review for approval.";

}

After successfully getting the KML string in to the database my next task was to get it to show up on the map for each point.  I built a webhandler (.ashx file) to handle requests for the KML string that dynamically generates the KML file structure. 

<%@ WebHandler Language="C#" Class="displayKml" %>

using System;
using System.IO;
using System.Web;
using System.Text;
using System.Data;
using System.Configuration;
using MySql.Data.MySqlClient;

public class displayKml : IHttpHandler {
   
    public void ProcessRequest (HttpContext context) {
        string intPointID = context.Request.QueryString["pointid"];

        MySqlConnection conn = new MySqlConnection(ConfigurationManager.ConnectionStrings["backcountryMySQL"].ToString());
        MySqlCommand cmd = conn.CreateCommand();
        string strKML = "";

        cmd.CommandText = "SELECT kmlstring FROM kml WHERE fkpointid = ?PointID";

        try
        {
            conn.Open();
            cmd.Parameters.Add("?PointID", MySqlDbType.Int32);
            cmd.Parameters["?PointID"].Value = intPointID;
            strKML = cmd.ExecuteScalar().ToString();
            conn.Close();
            conn.Dispose();

            //*************************************************************************
            //Download the KML file to client
            //*************************************************************************
            context.Response.Clear();
            context.Response.ContentType = "text/xml";
            context.Response.AppendHeader("Content-Disposition", "attachment; filename=" + intPointID + ".kml");
            context.Response.ContentEncoding = System.Text.Encoding.UTF8;
            byte[] byteArray = Encoding.ASCII.GetBytes(strKML);
            context.Response.BinaryWrite(byteArray);
            context.Response.End();

        }
        catch
        {
        }
    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }

}

Finally on the page that displays the google map and kml file I included a request to the webhandler to display the file in the initialization of the Google Map.

 <script type="text/javascript">
        function initialize() {
            if (GBrowserIsCompatible()) {
                var strLat = <%=strLat %>;
                var strLon = <%=strLon %>;
               

                var map = new GMap2(document.getElementById("map_canvas"));
                map.setUIToDefault();
                map.addMapType(G_SATELLITE_3D_MAP);

                //Zoom to the point
                var point = new GLatLng(strLat, strLon);
               
                if (<%=strShowMap %> == '0') //User does not have access to the map.
                {
                    var zoomLevel = 1;
                    map.setCenter(point, zoomLevel);
                }
                else //Show the map and KML file
                {
                    map.setCenter(point);
                    //View the KML
                    url_end = "?nocache=" + (new Date()).valueOf(); 
                    var geoxml = new GGeoXml("http://www.backcountrysecrets.com/displayKml.ashx?kmlstring=<%=kmlID %>");
                    map.addOverlay(geoxml);
                    geoxml.gotoDefaultViewport(map);
                }
            }
        }
</script>

 

Tags: , , ,

MySQL | ASP.NET | KML | Google Maps