Category Archives: .net

.NET – XML and XPath

I have been receiving a few requests for my now elderly Delphi XML/XPath examples to be brought into the world of .NET. Similarly, I have seen a lot of newsgroup posts about using XML and XPath expressions, particularly those XPath expressions that can be used to “query” the XML “database”.

The most popular request seems to have been for my “employee” selector demonstration:

It’s a small application that lets you load some XML (employee data) into an XML document. It then lets you fire a handful of XPath expressions at the XML document via the use of the SelectNode methods. It demonstrates selecting specific employees using a combination of conditions; there’s a mixture of XPath that looks at element values and one that looks at an attribute value. I’ve revamped it slightly, noteably I’ve made a few minor changes to bring it into line with current W3C standard (as enforced by .NET’s SelectNodes method).

[code lang=”XML”]




[code lang=”C#”]
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using System.Xml;

namespace XML
public partial class Form1 : Form
XmlDocument doc;

public Form1()

private void button1_Click(object sender, EventArgs e)
doc = new XmlDocument();

textBox1.Text = doc.OuterXml;

private void handle_xpath(String xPathExpression)
XmlNodeList result;
XmlNode root = doc.DocumentElement;

result = root.SelectNodes(xPathExpression);

label1.Text = String.Format(“{0} items returned”, result.Count);

foreach (XmlNode x in result)
textBox3.Text = textBox3.Text + x.OuterXml + Environment.NewLine + Environment.NewLine;

private void button2_Click(object sender, EventArgs e)

private void button3_Click(object sender, EventArgs e)

private void button4_Click(object sender, EventArgs e)

private void button5_Click(object sender, EventArgs e)

private void button6_Click(object sender, EventArgs e)
handle_xpath(“/employees/employee[emp_salary>30000 and emp_salary<35000]"); } private void button7_Click(object sender, EventArgs e) { handle_xpath("/employees/employee[emp_salary>30000 and emp_salary<35000 and emp_salary[@currency='UKP']]"); } private void button8_Click(object sender, EventArgs e) { handle_xpath("/employees/employee[emp_salary > 50000]/emp_lastname”);

private void button9_Click(object sender, EventArgs e)

The source code is available here [60k].

There’s a short 60 second movie of the application in use here [748kb]. Courtesy of TechSmith’s Camtasia.

Technorati Tags: , , , , , , , , ,

Chris Seary on Securing LINQ to SQL

Security expert, Chris Seary has written a thought-provoking piece about the changing role of the Database Administrator (DBA) now that database querying is becoming a feature in many .NET programming languages, via the use of Language INtegrated Query (LINQ). With developers writing code that effectively reaches into the database, it does present developers and DBAs with a cause for concern, especially where performance might be an issue. Chris discusses this problem and lays down the foundation for what it likely to be considered a future best practice.

On another note, Chris is now an independent consultant. If you need a security expert, give Chris a call. Check out his MSDN articles and slide decks. Chris recently spoke at DDD5 to a full-house, deliverying a good overview of his ‘Ten Top Tips for Securing Web Applications’.

Technorati Tags: , , , ,

On Gnostice, PDF creation, Delphi, Borland’s Turbo products

On the 22nd September 2006, Gnostice released version 2.41 of the PDFToolkit and eDocEngine for the [Delphi] VCL. As regular readers of this blog will know, I’m a great fan of Gnostice products – their PDF creation tools and components are a real boon to developers. Of course, this continued support for the Delphi VCL for Win32 and .net despite the divestment of the Borland IDEs, is confirmation that Gnostice passionately believe in the Delphi product name and the VCL.

Indeed, one need only look at the PDFToolkit VCL “road-map” here to realise that Gnostice are intent on producing an astonishingly feature complete PDF component set…writing for the VCL using the VCL – V3.0 looks to be awesome. Not surprisingly, there’s a similar roadmap for eDocEngine VCL – plans all the way up to V3.0 are well documented over here.

In an earlier post, I touched on the fact that Borland IDE divestment and the direction in which many component vendors seemed to be taking their products (towards .net and C#). Whilst this seems to still be the case, it’s worth noting that Gnostice are firmly focusing on three market spaces: the traditional Delphi VCL, .NET and Java. Their PDFOne .NET product provides support for PDF creation inside Borland Developer Studio 2005 and 2006 – with demonstrations being provided using the C# language, whereas their eDocEngine and PDFToolkit are fairly and squarely targeted at the VCL.

Of course now that the Turbo range of products are available, we see a renewed interest in the Delphi language and its direction. Knowing that Microsoft’s Express Editions are free, I can’t see the Turbo products being direct competition, therefore their existence presumably serves to further the Delphi language and provide heritage continuance (outside of the more expensive Borland Developer Studio-style all-encompassing product set). Given the career path of Delphi’s original architect, Anders Heilsberg (from Borland to Microsoft), this renewed interest might be what it takes to let Delphi and the Turbo products catch up with Microsoft. After all, here we are seeing much talk of .NET 3.0, yet Borland Developer Studio 2006 still only targets .NET 1.1. That said, Bob Swart makes mention of the next version of Delphi that will support .net 2.0, codenamed Highlander, over here.

I would like to see the Turbo products enjoy a shorter release cycle, providing them with an opportunity to target .net 2.0 and 3.0. With the next version of Visual Studio (Orcas) just around the corner, there’s not much time left to achieve this, but it should be a goal nonetheless.

Despite my earlier concerns about the future of Delphi and its language, it seems that there is a rekindled enthusiasm for the Delphi product set, fuelled more so by the reappearance of the Turbo mark that made Borland who they are today. Indeed it was the original Turbo range that provided the inspiration for the VCL and their [Borland] re-introduction of this famous branding will take the VCL to new heights – a point not missed by Gnostice who have plans in place for native VCL product releases/upgrades right into 2007.

eDocEngine VCL – comprehensive, generic, 100% VCL electronic document creation component suite for Borland® Delphi™ and Borland® C++Builder™.

PDFtoolkit VCL – powerful component set for Borland® Delphi™ and Borland® C++Builder™ to manage, manipulate, view, print, enhance PDF documents and process PDF eForms.

PDFOne .NET – 100% .NET component library to implement PDF based software solutions.

PDFOne Java – 100% Java library to implement PDF based software solutions.

Gnostice Plans for 2006
PDF Toolkit VCL – Road-map
eDocEngine VCL – Road-map
Turbo Downloads

Technorati Tags: , , , ,

Glasgow 26th September 2006 – TDD slide deck and code

Today saw me run my Test-Driven Development and Code Coverage session for the third time in as many months. Whilst the audience was fewer in number than usual, the interactivity made it a good session for me (and for the attendees from what I can gather). One thing that came out of this delivery was the need to examine user interface testing in more detail – something for 2007 I think.

The source code, crib notes and slide deck are available here.

JetBrains .NET development tools blog

I am pleased to see yet another company embracing the blogging phenomena – it [blogging] really does allow developers to reach out to their audience and communicate in a collaborative environment where things actually happen.

JetBrains have started a .NET tools blog, using none other than WordPress.

All the usual JetBrains folks are blogging:

Oleg Stepanov | Sergey Dmitriev | Alex Tkachman | Dmitry Jemerov | Kir Maximov | Sasha Maximova | Mike Aizatsky

It’s probable that you have heard of JetBrains, especially if you are using such products as ReSharper, dotTrace, IntelliJ IDEA or you may well be using their RSS reader OMEA.

They’ve also opened up access to the dotTrace 2.0 Early Access Programme (EAP). More can be found here.

Elementary execution timing using QueryPerformanceCounter

At some point in your career as a developer, you will need to optimise your code to make it run faster or more economically. If you have not been asked by a user to “make that report or feature run/finish faster”, hang on in there, your time will not be far off.

There are many ways of optimising your code such that it offers the user a more responsive experience (i.e. it runs or feels faster). You may well have implemented an algorithm in the knowledge that it is slow, but gets the job done (e.g. a bubble sort). You have then gone on to optimise the algorithm by replacing it with something that you know to be much faster (e.g. the quicksort). Alternatively, your application may have grown over a long period of time, some calculations are now relying on so much data that it is time to re-think how they work. Or, as is the case here, two factors have resulted in part of my application taking a performance hit:

  1. Growth of code that performs progressively cumulative calculations, i.e. calculate a & b, c is based on a percentage of (a+b), d is based on a percentage of (a+b+c), where a, b and c are recalculated when d is requested – yes I know it’s by no means the best way, however the code was reasonably elegant and more importantly, it was very understandable.
  2. Acceptance that the calculations work and that the code looks reasonably elegant, albeit the calculations take “some time” for relatively few records.

How did the need for optimisation raise its head? Well, in this case, simply adding 20-30 records to my application then viewing the on-screen reports was sufficient enough to make me augment the code behind the reports with an hourglass (waitcursor). Whilst the reports worked, the lack of user feedback after the hourglass appeared was of concern to me. For this particular application, 20-30 records is a little more than the average that I would expect it to have to cope with.

Elementary Execution Timing
Luckily, the Win32 API provides a couple of useful methods that we can use to “time” our function calls. Here’s a class that surfaces those methods for use in C#:

[code lang=”C#”]
using System.Runtime.InteropServices;

namespace uSimpleExecutionTimer
public class SimpleExecutionTimer
extern static short QueryPerformanceCounter(ref long x);
extern static short QueryPerformanceFrequency(ref long x);

private long counter1 = 0;
private long counter2 = 0;
private long freq = 0;

public long TickCount
get { return counter2 – counter1; }

public long TickFrequency
get { return freq; }

public long StartTick
get { return counter1; }

public long EndTick
get { return counter2; }

public double TickCountSeconds
get { return (counter2 – counter1) * 1.0 / freq; }

public bool Start()
return QueryPerformanceCounter(ref counter1) != 0;

public void Stop()
QueryPerformanceCounter(ref counter2);
QueryPerformanceFrequency(ref freq);

In use, it looks like this:

[code lang=”‘C#”]
SimpleExecutionTimer set = new SimpleExecutionTimer();



Granted, you might want to do something a little more scientific than a simple MessageBox.Show, however this is enough to give us our first clue relating to the execution time of the two Populate…() methods.

Before optimisation
When I measured the execution time of the two Populate…() methods in my un-optimised application, the TickCount was 14,870,258. With a QueryPerformanceFrequency of 3579545, that made for a delay of over 4 seconds. Given that there were only 20 records being processed, 4 seconds is unacceptable, even if the results are correct. With a few other things thrown into the equation, we were at nearly 30 seconds as noted earlier.

The problem with augmentation
Of course, whilst the use of QueryPerformanceCounter/SimpleExecutionTimer is the need to add extra lines of code to your application. Removing the lines of code often means “commenting out” ot physically deleting the lines – both of which involve you touching your code thus introducing the possibility of accidental error. Perhaps more obviously, adding extra lines of code to your application necessitates at least a partial re-compile.

The problem: well, without going into detail, I’ll save that for another posting/review, it was compounded calculations.
Basically, I had a series of about 15 calculations like so:

a = CalcA(); // Iterates over a recordset performing up to 15 calculations per record
b = CalcB(); // Iterates over the same recordset performing a different calculation, again 15 times per record
c = (a + b) + 5% of (a+b);
d = c + 10% of c

My application required access to a and b on their own, but also required access to c. Simplicity and neatness meant that my calculations, whilst elegant, were repetitive and rather slow. Fortunately I had identified this up front, and wanted to move the application to “feature complete” before spending time optimising it. Thus when my application requested the value of c it actually went and recalculated a and b. Twice. And then we have the calculation for d…you can see the repetition taking its toll, however the calculations are reasonably elegant and obvious.

The solution: I created a new class that performed the calculations once and once only. With the problem identified and a solution in place, how long did the two Populate…() methods take? Well, the results were somewhat good: the TickCount was only 75,000…fractions of a second. So fast, the populating of the two grids appeared instantaneous. So much so, I could have disabled the code that turned the hourglass on and off! (But I didn’t!)

Code augmentation (adding extra lines of code) is not too bad if you are looking to perform a series of benchmarking exercises. For example, a few years ago I did some work that benchmarked the XML Document Object Model (DOM) versus the Simple API for XML (SAX). I wrote a small application that used QueryPerformanceCounter to monitor the results of loading small and large XML documents whilst taking into account the effects of caching. In this scenario, code augmentation prove to be rather useful, the code itself was never going to reach “Production” and was merely used for the benchmarking exercise.

Anyway, I hope that you’ll find QueryPerformanceCounter of some use in your applications.

Alas poor Delphi, do I see a sharp future ahead?

[Warning: thought gathering rambling follows, coherence might be sacrificed.]

Following Tod Nielsen’s letter announcing that Borland would be divesting their IDE product lines, driving an even tighter focus on the Application Lifecycle Management (ALM) market, what future lies ahead for those of us who have been using Delphi for most of the last eleven years?

I know that I am not alone in having an opinion about this divestment, and it’s one that may result in a further blog posting. Even some Microsoft employees have a very heavy interest in Borland’s divestment plans, some who were/are very close to the Delphi scene.

However, the purpose of this posting to two-fold:

  1. to plug Gnostice’s PDFOne – their .net 1.1/2.0 PDF creation components. It’s a great product, I reviewed their earlier Win32 VCL offering, eDocEngine and found it to be totally awesome (as this review confirms!). However, and this is not a criticism by any means, but the PDFOne demonstrations are written using C#. I believe that this is decision to use C#, and Gnostice are not alone, is a key driver that will dictate the direction of the “Delphi Language”.
  2. to try and understand, whether in this posting or a follow up, what might happen to Delphi as a “language”, if all vendors in the .net space move their demonstrations and support over to C#

Gnostice have produced a product for use in the .net environment. Without splitting hairs, that means products like: Borland Developer Studio 2005/6 (Delphi 2005/6), Visual Studio 2003/5, Visual C#/VB Express Editions. Given that Delphi 200x targets .net 1.1, Gnostice were wise to produce assemblies for both .net 1.1 and .net 2.0. The demonstration application that is supplied with PDFOne is written using C#…which means that it will work with Delphi 200x (via its “C#Builder” or C# personality) and it will work in the Microsoft IDEs too.

Of the other vendors who are moving their components (demonstrations and documentation too) from the VCL to .net, what if a number of them started ignoring true Delphi and provided C# examples? I guess most of us would just muck in and convert the code on an as-needed basis, after all, it’s not very difficult converting C# examples into true Delphi code (and vice versa). Certainly it would make me think about whether I should be writing any new applications using true Delphi code.

The importance of the “Delphi Language” is gradually being eaten away by the almost omnipresent C#. There are folks out there who believe C# is the best thing since sliced bread. However, us folks in the Delphi world have enjoyed virtually everything that C# has to offer today. Granted language progression slowed down somewhat when Anders Heilsberg joined Microsoft (see his TechTalk here). C#, and parts of the .NET framework (especially 1.1) are not as well abstracted as people might think, especially those of us with a Delphi background, a point that I made during my recent lament about Borland and .NET:

…is .NET 2.0 up-to-date with regard to the needs of today’s developer? I’m not so sure: Delphi was the first product to simplify Windows-based development with its glorious abstractions around WndProc and the Windows messaging sub-system. No longer did we have to write code to create treeviews, add nodes, or draw them in special ways, etc. Win32 development with Delphi was almost reduced mouse clicks. Visual Studio for .NET 1.1 brought a similar IDE metaphor to the .NET world, however it lacked a number of Win32-style events that we in the Delphi world take for granted, such as the simple ability to have owner draw controls (have you never wanted to make root nodes in a treeview bold?) In .NET 1.1, it’s back to basics, you have to implement everything yourself (as described in these hoops and by the example here.)

So Delphi 200x IDEs may be relegated to being competition for Microsoft’s IDEs. Is that such a bad thing? Can the market withstand such competition? Well, Borland obviously have a view on the competitive aspects of their IDE – they’re selling it off. By selling it off, they are telling me two things:

  1. They’ve got something else to focus on, in this case ALM. Presumably this is the next big cash-cow and we all should be buying shares in any company that promises big things in the ALM space.
  2. They’re hoping that the potential buyer will work with developers and the market space, and will progress the IDE within the competitive environment. I certainly hope this is the case.

Delphi itself, as a product, has always enjoyed a lot of kudos because of its legacy support, i.e. applications written using previous versions would re-compile with newer versions. The only problem in this legacy support scenario is third party components. [Borland, please note, this is not a criticism] For years Borland have charged circa £1000 for an upgrade to the next major version or nearly £2000 for a new purchase. Now, I am a Delphi fanatic and have been for 10 years. That’s a lot of upgrade cash. I’ve lost track of the number of Delphi developers who recite the same story to me, especially at user group meetings and developer events.

It’s one thing migrating a vanilla Delphi application from one version to the next major version, it’s another thing to migrate an application that is “third party component heavy”. It’s another thing because we have to shell out for the third party components that match the version of Delphi we’re upgrading to. If you’re doing well, the third party component vendor(s) have kept up and their products are available when you need them…not months after the release of Delphi itself.

On the other hand, and keeping things equal, I like the Microsoft IDEs too. Whilst both Borland and Microsoft IDEs require huge amounts of desktop real estate (screen resolution), each of the respective IDEs have a number of features that make them a delight to work with. Granted, in terms of developer productivity, they are behind the likes of IntelliJ, although it’s authors are making great efforts to correct that as Rob Lally confirms in his write up here (expect a Resharper review to appear here and over at Scottish Developers real soon now!)

I wonder if the recent emphasis on C# signifies the demise of what we know as the “Delphi Language”? Two things will provide the answer: time and the new DevCo who take over the Borland IDE market space. Will there be developer consultation? Will there be a customer satisfaction survey that lets us suggest what language features to important to us? I certainly hope so.

Further Reading
Borland rides Segue on trip out of IDE biz
Borland gambles without developers
Marco Cantu’s Support Delphi blog posting.
DavidI’s posting is here.
Hopefully this posting is merely an April Fool’s joke?
Product Roadmap
Borland wants to be a Red Hat for developers

Visual C# Express Edition

I’ve been looking at Visual C# Express Edition, which according to this blog posting is now free forever. Specifically, I’ve been looking at the functionality differences between the Express Editions and the fully-fledged Visual Studio Professional et al.

However, before I start to blog about the differences, I thought it prudent to let you know the benefits of registering these free editions. When I registered my copy of Visual C# Express Edition, I was offered access to a portal that gave me the chance to download this little lot of goodies:

IconBuffet Studio Edition Icon Suite
A collection of over 100 professional, royalty-free stock icons from The IconBuffet Studio Suite collection contains core icons from several of IconBuffet’s most popular hand-crafted stock icon collections. As part of this promotion, you are free to use these icons in your desktop and web application development.

Microsoft Visual C# 2005 Express Edition – Build a Program Now! Document Microsoft® Visual C#™ 2005 Express Edition: Build a Program Now!
This fun and highly visual guide walks you through a complete programming project—a desktop weather-reporting application—from start to finish. You’ll learn how to put the lightweight, easy-to-use tools in Visual C# Express to work right away—creating, compiling, testing, and delivering your first ready-to-use program. You’ll get expert tips, coaching, and visual examples at each step of the way, along with pointers to additional learning resources.

Graphics Server .NET – Utility – Graphics Server .NET
Experience Interactive Chart Design with Graphics Server .NET. Learn how fast and easy it is to design a chart and integrate it with your Visual Studio 2005 application with our new Interactive Designer. Professional looking results, fast development and reasonable prices for both Win and Web Forms.

/n software IPWorks! ADO.NET Data Provider – Utility /n software IP*Works! ADO.NET Data Provider
Free single machine license of IP*Works! ADO.NET Data Provider. With IP*Works! you can use SQL queries and data objects built on the .NET Framework to easily access data for Internet development. Implements a standard Microsoft .NET Data Provider for accessing Email (POP, IMAP, SMTP), News (NNTP), and RSS.

WebPlanner 2005 – Utility – ComponentScience WebPlanner 2005
The scheduling component for ASP.NET 2.0. WebPlanner 2005 for ASP.NET 2.0 is a fully functional scheduling component for day, week, month and timeline rendering. WebPlanner provides drag and drop features and is compatible with Internet Explorer, FireFox, Mozilla and Opera. It also includes a MonthPlanner control as an extra bonus.

Corbis Images – Utility
An assortment of 250 royalty-free images from Corbis for inclusion in your web sites and applications.

There’s a good chance that more goodies will be made available, so it’s worth registering and worth periodically checking to see if any new stuff as been made available!

Trivial: Binary To Integer

In response to a web-forum question, I found myself writing a little C# to convert from a string (containing a binary number) into the integer representation of the same. It’s the kind of trivial coding problem that first year programmers would come up against… Of course, we could make it harder by stipulating that we cannot use System.Convert.

I’m sure that there are much better ways of doing this, and I’d be glad to see some of them appear in the comments for this post. There, a kind of challenge…If the quality of the comments is good enough, I might be able to offer a small prize if you are a UK resident. No promises though!

[code lang=”C#”]
public int BinToInt(string binaryNumber)
int multiplier = 1;
int converted = 0;

for (int i = binaryNumber.Length – 1; i >= 0; i–)
int t = System.Convert.ToInt16(binaryNumber[i].ToString());
converted = converted + (t * multiplier);
multiplier = multiplier * 2;
return converted;

In use:

[code lang=”C#”]
// remember SEFTU, sixteen, eight, four, two, one
listBox1.Items.Add(BinToInt(“00001”).ToString()); // 1
listBox1.Items.Add(BinToInt(“00010”).ToString()); // 2
listBox1.Items.Add(BinToInt(“00011”).ToString()); // 3
listBox1.Items.Add(BinToInt(“00100”).ToString()); // 4
listBox1.Items.Add(BinToInt(“00101”).ToString()); // 5
listBox1.Items.Add(BinToInt(“00111”).ToString()); // 7
listBox1.Items.Add(BinToInt(“01000”).ToString()); // 8
listBox1.Items.Add(BinToInt(“10000”).ToString()); // 16[/code]

It might be of use to somebody…somewhere…

Selectively removing checkboxes in a .NET 1.1 / 2.0 TreeView

Earlier this month, I had the need to customise a TreeView control such that it had checkboxes against some, not all, of the nodes.

Here’s a screenshot of what I wanted:


It requires a little bit of code to achieve this effect, but is was worth the effort. Here’s the code that performs the magic:

[code lang=”C#”]
using System.Runtime.InteropServices;

namespace Treeview___CheckBoxes
public partial class Form1 : Form
public Form1()

private void Form1_Load(object sender, EventArgs e)
// Iterate over the root nodes, removing their checkboxes
for (int n = 0; nTechnorati Tags: , , ,