Wednesday 17 December 2008

XPath in C#

string url = "http://www.example.com/somedata.xml";

HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
StreamReader sr = new StreamReader(request.GetResponse().GetResponseStream());

string xmlstr = sr.ReadToEnd();

XmlDocument doc = new XmlDocument();
doc.LoadXml(xmlstr);

XmlNode root = doc.DocumentElement;

// Get a single node
XmlNode tnode = root.SelectSingleNode("/FEED/SOURCE");

// Get all nodes that match a certain XPath expression
XmlNodeList alltitles = root.SelectNodes("/BOOK/TITLE");

There is a useful table of XPath syntax on this page.

Thursday 11 December 2008

BBC iPlayer is stealing my bandwidth (and slowing down my computer)

If you're a BBC iPlayer user and like to download programmes to your computer rather than streaming them, you will have downloaded the BBC iPlayer Download Manager. What the BBC didn't tell you (or not very clearly, at least) is that when you install the Download Manager, it creates a Windows Service running in the background on your PC which is constantly making use of your internet connection (even when you don't have the Download Manager running) to upload chunks of the programmes you've got downloaded to your library to other iPlayer users.
Don't panic, this is not neccessarily a bad thing - in fact when you downloaded those programmes you did so from other users who were uploading them through the Windows Service running on their PCs - but a few weeks ago my computer started running slower than normal and I was finding that streaming iPlayer videos would stop and re-buffer every few seconds. With a quick Google search I found that it could be caused by this background behaviour of the iPlayer software. There were a few suggestions for how to stop this invisible uploading but the correct method is to do the following:

Open the Windows Services Manager by going to Start > Run... (or Start > Search box on Vista) and typing services.msc and pressing Return.
Scroll down until you find KService.exe. Right-click on it and click Stop. When it has stopped, right-click on it again and select Properties. In the dialogue that appears change the Startup Type drop-down menu to Manual or Disabled (Automatic means it will start again next time you reboot which we don't want).

That's all there is to it. Clearly if everyone does this, the peer to peer system that allows you to download iPlayer programmes won't work as there'll lots of downloaders but no uploaders but it's worth knowing if, like me, the iPlayer Download Manager starts causing you problems.

Wednesday 26 November 2008

Blank map markers for Google Earth


http://maps.google.com/mapfiles/kml/paddle/red-blank.png

http://maps.google.com/mapfiles/kml/paddle/ylw-blank.png

http://maps.google.com/mapfiles/kml/paddle/blu-blank.png

http://maps.google.com/mapfiles/kml/paddle/orange-blank.png

http://maps.google.com/mapfiles/kml/paddle/grn-blank.png

http://maps.google.com/mapfiles/kml/paddle/purple-blank.png

http://maps.google.com/mapfiles/kml/paddle/wht-blank.png

Best method to read a web page into a string in C#

There are a number of ways to get the contents of a web page (or xml document such as an RSS feed) into a string in C#.

Unless someone can show me an even shorter method, I think the least lines of code way to do this is using the WebClient class in the System.Net namespace as follows:

WebClient client = new WebClient();
string pageContents = client.DownloadString(url);

Job done!

Note that this is much shorter than the method you're most likely to find on Google which is to build a web request then process the resulting response stream into a string using a StreamReader as in the below example. This works but is a lot more lines of code and creation of unnecessary objects.

using System;
using System.IO;
using System.Net;
using System.Text;

public static void GetFile(string strURL, string strFilePath)
{
WebRequest myWebRequest = WebRequest.Create(strURL);
WebResponse myWebResponse = myWebRequest.GetResponse();

Stream ReceiveStream = myWebResponse.GetResponseStream();
Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
StreamReader readStream =
new StreamReader( ReceiveStream, encode );
string strResponse = readStream.ReadToEnd();

readStream.Close();
myWebResponse.Close();

}

Wednesday 19 November 2008

Samsung Omnia firmware update for UK Vodafone users

After much complaining from Omnia owners on the Vodafone support forum (many people were stuck on the HH3 "battery killer" firmware), Vodafone have finally released a firmware update (through Samsung) for the Omnia (i900) to bring it up to date with the updates that Samsung have been releasing.

The new firmware (version HJ) is officially approved by Vodafone so no worry about voiding your Vodafone warranty (installing Samsung firmwares that are not approved by Vodafone will result in them abandoning you) and can be downloaded from here.

Cannot resolve the collation conflict

Joining two tables on a name column...
select * from tableOne as a
join tableTwo as b
on
a.name = b.name
...came up with this error...
Msg 468, Level 16, State 9, Line 1
Cannot resolve the collation conflict between "Latin1_General_CI_AS" and "SQL_Latin1_General_CP1_CI_AS" in the equal to operation.
Solution was to change the collation of the appropriate column:
select * from tableOne as a
join tableTwo as b
on
a.name COLLATE Latin1_General_CI_AS = b.name

The message(s) could not be sent - Windows Mobile 6.1 SMTP email bug

I came up against this message yesterday when trying to send an email through gmail from my Samsung Omnia:
The message(s) could not be sent. Check that you have network coverage and that your account information is correct. Then try sending again.
Sending email had worked before but now didn't. I checked all the smtp settings and they were all correct. A bit of web-hunting found that this is a problem with all Windows Mobile 6.1 devices - if at any time a connection to the smtp server fails for some reason, the email account gets corrupted and will no longer send email. The account would then have to be deleted from the device and set up again from scratch.

There are unofficial fixes for this around on the web to avoid having to re-setup the email account but Microsoft have released an official patch which just involves downloading a .cab file from here and installing it on your mobile.

Tuesday 18 November 2008

The endpoint you entered was not correct

Solution to Flickr problem with linking to WordPress using xmlrpc.php file.

I was trying to set up my Flickr account to automatically upload to a WordPress blog but having enabled XML-RPC in the WordPress control panel, Flickr kept giving the error "The endpoint you entered was not correct". Navigating to the xmlrpc.php file (http://yourblogpath/xmlrpc.php) in a browser returned a 403 Forbidden error whereas it should show a page saying "XML-RPC server accepts POST requests only".

Googling around suggested a number of potential solutions such as trying again until it works and trying in Internet Explorer (rather than Firefox) but the one that fixed it was to add the following code to the .htaccess file in the blog root and then try again:

<files>
SecFilterInheritance Off
</files>

(Final solution found here)

Resolve the branch for all nodes in the node tree

SQL to resolve the path from the root to the particular node in both node name and node id form.
(Demonstrates cursors, converting int to string, concatenating strings and selecting into variables)

drop table NodePaths

create table NodePaths
(Id int,
NamePath varchar(1000),
IdPath char(500)
)

declare @thisname as varchar(200)
declare @fullpath as varchar(1000)
declare @idpath as varchar(500)
declare @startid as int, @pnid as int, @currentid as int
declare @thisid as varchar(10)

declare c_1 cursor for
select id from node

open c_1
fetch c_1 into @currentid

while @@fetch_status = 0
begin

set @startid = @currentid

select @pnid = parentnodeid, @fullpath=[name], @idpath = id from node where id = @startid

while @pnid <> -1
begin
select @thisname = [name], @thisid = CONVERT(varchar(10),id), @pnid = parentnodeid from node where id = @pnid
set @fullpath = @thisname + ' > ' + @fullpath
set @idpath = @thisid + ' > ' + @idpath
end

insert into NodePaths
select @startid,@fullpath,@idpath

fetch c_1 into @currentid
end

close c_1
deallocate c_1

Wednesday 12 November 2008

Transfer SQL table to different schema (eg dbo)

The SQL command to assign a table to a different schema is:

ALTER SCHEMA newSchemaName TRANSFER [OldSchemaName].MyTable

This can therefore be used to assign tables created under a particular user to dbo (which can be necessary as sometimes stored procedures may fail with "Invalid object name" if tables are not under dbo) as follows:

ALTER SCHEMA dbo TRANSFER [OldSchemaName].MyTable

Matlab fatal error on startup

Had the situation where Matlab fails as soon as you open it and posts a very unhelpful message - "Fatal error on startup" with no useful extra information.

A hunt on Google suggested renaming the folder with the matlab.prf preferences file in it (see matlab support here) so that Matlab creates a new one on startup. That didn't help.

Next thing Google suggested was killing the spoolsv.exe (windows printer spooling service) process in task manager. This allowed Matlab to be opened successfully but I don't want to have to choose between Matlab and printing!

Further investigation found that changing the default printer to a different one on the network allowed Matlab to be opened once spoolsv.exe had come back. Assume that Matlab tries to connect to the default printer on startup but the original default printer was unreachable so Matlab just falls over. Problem is solved by simply changing to a different printer.