Thursday, 31 July 2008

Load XML doc from URL

Really easy :)

XmlTextReader rssReader;
XmlDocument rssDoc = new XmlDocument();
rssReader = new XmlTextReader(rssFeedURL);

Thats it!

Friday, 18 July 2008

Writing a DataSet to a Text File

DataSet versions = new DataSet();
string versionFilePath = Path.Combine(tempDir, "Versions.xml");
FileStream fs = new FileStream(versionFilePath, FileMode.CreateNew);

This writes out a text file containing the data schema and the data.

Friday, 4 July 2008

Control Adapters

Had my first run it with ASP.Net control adapters this week. I was using the built in Menu control which was unfortunately rendering as a table. I needed a menu that rendered as a list using <li> tags instead.

Up to the plate stepped control adapters. These let you mess with the presentation layer of the control, in this case change a menu from rendering <Table> tags to <li>.

Here's how...

Add a App_Browsers folder to the project, you can do this from the right click menu.
Add in a "Form.browser" file. This is an XML file that tells the ASP system what class to use instead of the menus presentation layer. Here's a sample.

<browser refID="Default">
<adapter controlType="System.Web.UI.WebControls.Menu" adapterType="TDWeb.MenuControlAdapter" />

The "TDWeb.MenuControlAdapter" is the Namespace and class name.

namespace TDWeb {

#region Namespace references

using System;
using System.Web.UI;
using System.Web.UI.Adapters;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.Adapters;


public class MenuControlAdapter : MenuAdapter {

#region Methods

private bool MenuItemContainsSelectedItem(MenuItem menuItem) {

if ( menuItem.Selected )
return true;

foreach ( MenuItem childItem in menuItem.ChildItems ) {

if ( this.MenuItemContainsSelectedItem(childItem) ) {
return true;


return false;


protected override void OnPreRender(EventArgs e) {

if ( this.Control != null ) {




protected override void RenderContents(HtmlTextWriter writer) {

if ( this.Control != null ) {

foreach ( MenuItem menuItem in this.Control.Items ) {
this.RenderMenuItem(menuItem, writer, 0, true);



private bool RenderMenuItem(MenuItem menuItem, HtmlTextWriter writer, int level, bool withArrows) {

bool selected = this.MenuItemContainsSelectedItem(menuItem);

if ( this.Control.StaticDisplayLevels >= level ) {

// Render each item as a
  • ...

  • //writer.RenderBeginTag(HtmlTextWriterTag.Li);
    writer.Write(@" if (menuItem.Selected)
    writer.Write(@" class=""selectedmenuitem"" ");
    //writer.AddAttribute(HtmlTextWriterAttribute.Class, "selectedmenuitem");
    // Current item should be styled with "selected" class ...
    //if ( selected )
    // writer.AddAttribute(HtmlTextWriterAttribute.Class, @"qmparent");

    // If a description was specified, add it as a tooltip ("title" attribute) ...
    if ( menuItem.ToolTip != string.Empty )
    writer.AddAttribute(HtmlTextWriterAttribute.Title, menuItem.ToolTip);

    // Render the "href" attribute ...
    //if ( !menuItem.Selected )
    writer.AddAttribute(HtmlTextWriterAttribute.Href, menuItem.NavigateUrl);

    //writer.AddAttribute(HtmlTextWriterAttribute.Class, "selectedmenuitem");

    //// Render the "A" element ...
    //if (level == 0)
    // writer.AddAttribute(HtmlTextWriterAttribute.Class, "qmparent");

    //if ( withArrows )
    // writer.WriteEncodedText(@" | ");

    // Now render any child items ...
    if ( this.Control.StaticDisplayLevels > level ) {
    if (menuItem.ChildItems.Count > 0 ) {
    foreach ( MenuItem childItem in menuItem.ChildItems ) {
    this.RenderMenuItem(childItem, writer, level + 1, false);

    // Close the
  • ...
  • tag ...

    return menuItem.Selected;


    private void SelectCurrentPage(MenuItemCollection menuItems) {

    Uri rawUrl = new Uri(this.Control.Page.Request.Url, this.Control.Page.Request.RawUrl);
    this.SelectCurrentPage(menuItems, rawUrl);


    private void SelectCurrentPage(MenuItemCollection menuItems, Uri rawUrl) {

    foreach ( MenuItem menuItem in menuItems ) {

    string navigateUrl = this.Control.ResolveUrl(menuItem.NavigateUrl);
    Uri targetUrl = new Uri(rawUrl, navigateUrl);
    menuItem.Selected = string.IsNullOrEmpty(menuItem.NavigateUrl = rawUrl.MakeRelativeUri(targetUrl).ToString());

    this.SelectCurrentPage(menuItem.ChildItems, rawUrl);






    Wednesday, 2 July 2008

    Making a Webpage look the same in Firefox and IE

    The problem : the styling works in Firefox but doesn’t in IE6.

    Here’s how to get round many of the problems. Initially build the site so that it works in Firefox. Then look at it in IE. If it looks different then add a second stylesheet that’s only read by IE.

    Example : In the below image the shaded bar should reach to the edge of the blue box. In Firefox it does reach the edge, but as shown here in IE, it does not.

    Here’s the style sheet reference that controls the width of the bar…



    background-image: url(../images/menu_back.gif);



    So to get it to work in IE, add a second stylesheet and add an override to correct the problem…




    Note that the IE stylesheet does not replace everything, it is simply overriding one attribute in the stylesheet.

    Next you have to add a special link to the new style sheet like this.

    <link href="Styles/P.css" rel="stylesheet" type="text/css" />>
    <!--[if gte IE 6]>
    <link href="styles/Pie6.css" rel="stylesheet" type="text/css" />

    The second stylesheet (IE ONLY) is hidden from other Browsers by putting its link statement in the HTML comment as shown.

    Tuesday, 24 June 2008

    Creating a simple Computed Column in SQL2005

    CREATE TABLE [dbo].[CustomerType](
    [iCustomerTypeID] [int] IDENTITY(1,1) NOT NULL,
    [vcDescription] [varchar](50) NOT NULL,
    [vcMore] [varchar](50) NULL,
    [LookUpDescription] AS
    ) Persisted NOT NULL

    In this example the LookUpDescription column mimics the contents of the vcDescription field. Could have been concatenated fields or written a case statement or pretty much anything else. These columns do not take up storeage and CAN BE INDEXED!

    So they will have a cost as far as indexing, and returning the results are concerned.