Writing on Tablets Tech stuff from @yeltzland

Copying files needed for Visual Studio 2005 Unit Tests

Originally posted on my old MSDN blog

I'm not 100% convinced that the unit test framework in VS2005 is a step forward from using NUnit.

I don't like the interface (in a shallow way I really like the progress bar of NUnit, and the feeling of well-being it gives when it goes green!), and I really hate the way it creates a new directory of files each time tests are run. I can see how that's useful when you're running functional or acceptance tests, but I run unit tests tens or hundreds of times a day, and the disk space that uses up is crazy.

Anyway, one thing I did learn is how to copy test asset files into the Out directory so it can be referenced by the test code. This foxed me for a while, as not unreasonably you would expect the "Copy To Output Directory" property on the file would do the job. However that will copy to the bin\Debug directory on building, but not copy to the Out directory when the tests are run.

The solution is to add the required file to the test run configuration, which is accessed from the Test -> Edit Test run Configurations -> [your config file] menu, and then on the Deployment tab.

Visual Studio Unit Tests

Windows ISA Client 2004 has a browser tab!

Originally posted on my old MSDN blog

I'd never seen it before, and it turns out hardly anyone else in our team had either so I thought it was worth sharing.

I was having problems accessing a particular resource on our internal network for which I had to use a particular proxy server. I set the proxy in the manually set ISA server and fired up IE, but I couldn't see the resource. If I set the IE proxy settings manually (Tools -> Internet Options -> Connections -> LAN Settings) I could see it fine, but this confused me - unfortuanately not a common occurrence  :-)

What I'd never noticed was a second tab on the ISA Client called "Web Browser", on which the "Enable Web browser automatic configuration" checkbox was unchecked. Once I checked this, obviously then changing the proxy settings also worked in IE.

Not exactly earth-shattering, but I learnt a little (so I'm allowed to post it here!)


Using SelectSingleNode (or SelectNodes) on XML where the default namespace has been set

Originally posted on my old MSDN blog

I've been stumped by this one at least two times over the last couple of years, so I thought it was a good candidate to be written up here.

I was trying to select a node from some standard XHTML where the default namespace was set. In otherwords the XHTML was something like:

!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"[]>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>MSN Search News: Microsoft</title> ...

Note the xmlns attribute on the root <html> node.

Without thinking too hard, I first tried to find the title of the page by going ...

XmlDocument resultsXhtml = new XmlDocument();
XmlNode metaNode = resultsXhtml.SelectSingleNode("//title");

... which left metaNode as null.

This took me a little while to figure out. Clearly I need to identify in the XPath query that the title tag is in the default namespace, but how can I do that if that namespace has no prefix in the actual XML.

The solution (reasonably obviously!) is to register a prefix of my own choosing in an XmlNamespaceManager object, and then use that namespace manager when doing the select. Here's some code that works:

XmlDocument resultsXhtml = new XmlDocument();
XmlNamespaceManager namespaceManager = new XmlNamespaceManager(resultsXhtml.NameTable);
namespaceManager.AddNamespace("myprefix", "http://www.w3.org/1999/xhtml");
XmlNode metaNode = resultsXhtml.SelectSingleNode("//myprefix:title", namespaceManager);

I think what's interesting about this problem, is the way you have to think about namespaces and XPath queries. The namespace is a logical entity denoted by the URI not the prefix in the actual XML. Therefore you can register that URI with any prefix you want in your XPath, which isn't a completely intuitive concept - to me at least!

XML Namespaces C#