Thursday 31 July 2008

Load XML doc from URL

Really easy :)

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

Thats it!

Friday 18 July 2008

Writing a DataSet to a Text File


DataSet versions = new DataSet();
versions.Tables.Add(newTable);
string versionFilePath = Path.Combine(tempDir, "Versions.xml");
if(File.Exists(versionFilePath))
{
File.Delete(versionFilePath);
}
FileStream fs = new FileStream(versionFilePath, FileMode.CreateNew);
versions.WriteXml(fs,XmlWriteMode.WriteSchema);
fs.Close();
fs.Dispose();


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.

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

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;


#endregion

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) {
base.OnPreRender(e);

if ( this.Control != null ) {

this.SelectCurrentPage(this.Control.Items);

}

}

protected override void RenderContents(HtmlTextWriter writer) {

if ( this.Control != null ) {

writer.AddAttribute(HtmlTextWriterAttribute.Id,"qm0");
writer.AddAttribute(HtmlTextWriterAttribute.Class,"qmmc");
writer.RenderBeginTag(HtmlTextWriterTag.Ul);
foreach ( MenuItem menuItem in this.Control.Items ) {
this.RenderMenuItem(menuItem, writer, 0, true);
}
writer.RenderEndTag();

}

}

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");
    }
    writer.Write(@">");
    // 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");
    //}
    writer.RenderBeginTag(HtmlTextWriterTag.A);
    writer.WriteEncodedText(menuItem.Text);
    writer.RenderEndTag();



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

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

    // Close the
  • ...
  • tag ...
    writer.Write(@"");
    //writer.RenderEndTag();
    }

    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);

    }

    }

    #endregion

    }

    }

    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…

    .menubar
    {

    width:724px;
    text-align:left;
    clear:left;

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

    background-repeat:repeat-x;
    font-weight:bold;
    height:25px;
    padding-left:18px;

    }


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

    .menubar
    {

    width:742px;

    }


    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" />
    <![endif]-->



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