Thursday 25 October 2012

Processing xml fragments which include namespaces in C#

Recently I needed to process an xml fragment using .Net's XmlReader class. This worked fine until the fragment contained a reference to a namespace when it started throwing undeclared namespace exceptions. In my case the namespace was xmlmime which is not included in the default namespace manager.

After searching the web for examples I found that there was a general lack of examples which use XmlReader. Mostly they use XmlTextReader which for various reasons I was unable to use in the project I was working on. Below is an example snippet of code which show how to overcome the undeclared namespace exception I was getting when trying to read the fragment.

byte[] xmlBytes = System.Text.Encoding.UTF8.GetBytes(xmlString);
MemoryStream stream = new MemoryStream(xmlBytes, false);

// Create the namespace required to process configuration xml objects
XmlNamespaceManager namespaceManager = new XmlNamespaceManager(new NameTable());
namespaceManager.AddNamespace("xmlmime", "http://www.w3.org/2004/11/xmlmime");
namespaceManager.AddNamespace("another", "urn:something");

// Create the context required to parse configuration fragments
var context = new XmlParserContext(null, namespaceManager, null, XmlSpace.Default);

// Create the xml reader with the required context to parse a configuration object
var xmlReader = XmlReader.Create(stream, null, context);

while (xmlReader.Read())
{
 switch (xmlReader.NodeType)
 {
  case XmlNodeType.Element:
   // process element
   break;
 }
}

Monday 3 September 2012

Uninstalling SQL Server from Windows 2008 R2 server core

How hard can it be to uninstall SQL Server from Windows Server 2008 R2 core?

It should be straight forward but first I had to find the magic uninstall command - setup.exe which is located in the Setup Bootstrap\SQLServer2012 folder in the folder that SQL server 2012 was installed in to in the first place. On my machine this is C:\Program Files\Microsoft SQL Server\110\Setup Bootstrap\SQLServer2012.

It took me some time but eventually I found the following command worked
setup /action=uninstall /FEATURES=SQL,Tools,RS,AS,IS /INSTANCENAME=MSSQLSERVER /UIMODE=EnableUIOnServerCore.
I tried using /QS instead of /UIMODE=EnableUIOnServerCore but it couldn't find any features to uninstall!

It also allowed me to select all of the components including the shared components - which is the only way I found to remove all of them apart from the native client.

To remove the Native Client use:
msiexec /uninstall {49D665A2-4C2A-476E-9AB8-FCC425F526FC}

It does leave a few log files and empty directories floating around but they are easily cleaned up.

Monday 2 July 2012

Recovering a Windows 2008R2 server with a dead motherboard

Background

Earlier this week our Windows 2008 R2 based domain server started having random crashes and then finally it would not even restart to the BIOS screen.

We have regular backups so we were not unduely worried and thought that this would be an ideal time to update the server hardware especially as we didn't have any hardware that was remotely similar to the existing server.

Method

My first attempt was to make a copy of the disk (all the partitions using Clonezilla) and the try and boot the new hardware from this copy. Unfotrunately all that happened was the monitor would flash and the machine restarted to the recovery options screen. It didn't seem to matter what options I selected it made no difference.

Next I tried booting to DSRM and restored the server with exactly the same result!

Finally, I copied the disk again and this time I used the information I found after searching the internet some more in Mark Minasi's forum. He has a lot of very interesting information in his forums for sys ops and I should have remembered to chek there first. His descriptions are usually much much better than Microsofts.

It turned our all I needed to do in the recovery console was:

  1. do bcdedit /v to get the GUID of the OS entry you're booting.
  2. With that guid, type
    bcdedit /set {FILLINTHEGUIDHERE} detecthal on
  3. Ran regedit
  4. Navigated to HKLM
  5. Selected File, Load Hive (Note: Must be under HKLM first, or this option will be greyed out)
  6. Navigated to C:\Windows\System32\Config\SYSTEM
  7. Loaded hive HKEY_LOCAL_MACHINE (which mounts as a sub-hive)
  8. changed Restored_HKLM\ControlSet001\services\msahci\Start to 0
  9. Select File, Unload Hive (may need to select the hive node in HKLM)
  10. Restart

And it WORKED. All I had to do now was install some missing drivers off the new computers CD and re-activate the licence.

Conclusion

The steps are very simple once you know how. The hardest bit of this whole thing was actually getting the information.

Monday 18 June 2012

Raspberry Pi musings part 3

This week I managed to sort out my Raspberry Pi power supply problems. I purchased a Nokia N97 UK 3 Pin Mains Charger AC-10X and this seems to do the trick. The Raspberry Pi seems quite sensitive to what is powering it.

This weekend I decided that since a version of RiscOS was available for the Raspberry Pi, I would give it a go and see how it compares to old Acorn hardware.

I know it's pre-alpha but unfortunately I have failed to get it to load. The display gets a signal so that it comes out of powersave mode but no picture, the keyboard LEDs come on and the mouse lights up, but that's as far as it gets. The screen is getting a signal but the keyboard doesn't respond.

I tried updating the boot loaders etc but this only makes things worse. With the new loaders the screen does not even get a signal and nor do the keyboard or mouse.

I'm going to give up on RiscOS at the moment and switch back to spending the little time I have for playing to tinkering in Linux until RiscOS has a more stable release.

Sunday 27 May 2012

Raspberry Pi musings part 2

This morning I've been trying to load the root file system off a hard disk.

This is not as easy as it would first seem. I tried a couple of portable harddisks and the Raspberry Pi (when booted from the standard SD card) didn't recognise them.

Finally it tried an oldish 70GB 3.5" sata drive I had lying about, connected it to a cheap USB to SATA cable and to my surprise - it worked.

The next step was to copy the root partition from the SD Card onto the drive which I did using my ubuntu virtual machine, so that I wasn't trying to copy a live system. I then checked on the RPi that the partition was readable and error free.

Next I altered the SDCard boot to boot from /dev/ada1 (which is where the harddisk is) and then rebooted. Unfortunately, it did not work. I got error messages syaing that the filesystem was corrupt. Which when I checked, sure enough it was! Something in the boot process is corrupting the disk.
Edit: I've found out what was causing the corruption - using the HDMI cable. If I connect to the Raspberry Pi remotely it works fine. It looks like I need to get a better power source.

I've now manually created an ext2 file system on the disk and used rsync to copy the files from the second partition of the SD card and it now works properly. I've got my Raspberry PI booting from a hard disk.

It seems to have a reasonable transfer rate - ~25MB/s for writing and ~23MB/s reading.

pi@raspberrypi:~$ dd if=/dev/zero of=test.tmp bs=1M count=1024
1024+0 records in
1024+0 records out
1073741824 bytes (1.1 GB) copied, 42.1622 s, 25.5 MB/s
pi@raspberrypi:~$ dd of=/dev/null if=test.tmp bs=1M count=1024
1024+0 records in
1024+0 records out
1073741824 bytes (1.1 GB) copied, 45.6714 s, 23.5 MB/s

Saturday 26 May 2012

Raspberry Pi musings

I'm one of the lucky people who've received their Raspberry Pi (see http://raspberrypi.org.uk).

I ordered it on the launch day 29th February and I received it about 10 days ago but I've not really had a chance to play with it until today.

Initially I tried creating the SD cards on Windows 7 (64 bit), but after fighting the Windows authority system I gave up and installed Ubuntu server as a VMWare virtual server which I used to create the card images.

My initial feelings are that it is very sensitive to power supply and usb port load. My old wired iMac keyboard works OK if it is the only thing connected but plug in the ethernet cable or a mouse and it starts doing wierd things and the network misbehaves as well. I solved this by plugging the keyboard and mouse into a powered hub and now everything seems to work as expected.

I think the Raspberry Pi doesn't like any of my SD Cards because they all seem to be only getting approximately 720kB/s (using dd on the stock debian distro with the updated firmware) but the same card used in my VMWare virtual Ubuntu server has a transfer rate of 4.5MB/s.

For my second experiment I'm going to try and have only the bootloader on the SDCard and the rest on a harddisk and see how that setup performs.