Get-Content and –encoding Switch

Today I ran into a funny issue. I was playing around with PowerShell in order to automate some repetitive task I have to do at work (it is Web UI automation, I’ll post on that later), and when reading XML file using Get-Content I had encoding issues. When the XML contained a ü character, it was replaced in the output file by some junk that immediately tells you that there is something wrong with encoding somewhere.

Let’s see this with a little example.

Here is a sample XML file:

<Test>
 <EvilStringOfDeath><![CDATA[
  a'b<'>",!"/%$?$&?%*(()%/"!"/&?%$/"*&$/"?%&?-f¯Ñ112üêù
 ]]></EvilStringOfDeath>
</Test>

Ok, the content is ugly, but you get the point. Let’s read this in PowerShell and see what happens

$xmlData = [xml] (gc .\Input.xml)

And now, let’s see the content of that variable

$xmlData.Test.EvilStringOfDeath.get_innertext()

a’b<’>",!"/%$?$&?%*(()%/"!"/&?%$/"*&$/"?%&?-f¯Ò112üêù

Something was lost in translation. You might think that it’s only a display issue, as help on Get-Content says that the –encoding switch but it is not. If you write the content of the $xmlData variable using .Save() or simply by out-putting it to a file (using > operator), the content of the file will not be valid.

To make this work, there is a switch in Get-Content: -encoding. You can specify which encoding format. So, using this command:

$xmlData = [xml] (gc .\Input.xml -enc UTF8)

The data should be read properly. Now if I display the content of $xmlData once more, here is what I get:

a’b<’>",!"/%$?$&?%*(()%/"!"/&?%$/"*&$/"?%&?-f¯Ñ112üêù

Perfect, I got what I want. I can now save or output it properly!

PS: if you go a gm (Get-Member) on $xmlData.Test.EvilStringOfDeath you won’t see the get_innertext() member. I don’t know why, but still, it works. I found it over here.

PS2: I had that issues at work, when I’m running a XP laptop. Now that I’m home on my Windows 7 RC box, the –encoding switch of Get-Content doesn’t even show in the help… There sure is a good explanation, but I don’t have it.

Windows 7, SQL Server 2008 and Subtext, part 2

Here is the follow up of my post on building Subtext project with Visual Studio 2008 under Windows 7, with SQL Server 2008 Express installed.

In this post, I’ll explain how to deploy Subtext from Visual Studio on the local IIS server.

So, using the “Publish…” option in Visual Studio, let’s deploy the Subtext.Web project on the local IIS.

VS9SubtextPublish

VS9SubtextPublishToLocalIIS

The project will then build and be published, but the process will fail. There is a file missing in the SVN repository:

Publishing folder Skins/Colors…
Unable to add ‘Skins/Colors/skins.config’ to the Web site.  Unable to add file ‘Skins/Colors/skins.config’.  The system cannot find the file specified.

In the Subtext.Web project, you can see the missing file:

VS9SubtextPublishMissingFile

Solution to this is very easy:

  1. Delete the reference to the file in the solution (simply select the file, as in the picture above, and press delete).
  2. In the same panel, click on “Show All Files” button.
  3. Select the “skin.config” file that is now displayed, right click on it and select “Include In Project”.

What’s happening here is that there is a sync issue between the Visual Studio project file and the files on the file system. The project file references a “skins.config” file that doesn’t exist on the file system.

It is time to publish the project, again. Fails, again :)

Publishing folder Skins/Lightz/Scripts…
Unable to add ‘Skins/Lightz/Scripts/cityNight.js’ to the Web site.  Unable to add file ‘Skins/Lightz/Scripts/cityNight.js’.  The system cannot find the file specified.

Unable to add ‘Skins/Lightz/Scripts/tableEffects.js’ to the Web site.  Unable to add file ‘Skins/Lightz/Scripts/tableEffects.js’.  The system cannot find the file specified.

Same kind of issue as before, but this time the files don’t exist on the file system.

VS9SubtextPublishMissingScripts

This time, simply delete the two files from the project.

Publish again, this time it works! (Note that it worked the two previous times, but there was an error from Visual Studio as the files were not found. As the missing files were only scripts or configuration for skins, this did not prevent Visual Studio from deploying the rest of the project).

Browsing the local Subtext

Publishing was the easy part. Now, if you browse to http://localhost/Subtext, you will get an error:

Server Error in ‘/Subtext’ Application.
Object reference not set to an instance of an object.

I took me a while to spot what was wrong this time. The project file explicitly says to not copy the “Web.config” file to the output folder. So, when you publish from Visual Studio, there is no “Web.config” file copied along…

In the “Web.config” properties window, change the “Build Action” from “None” to “Content”.

VS9SubtextPublishWebConfigContent

There we go. Let’s publish again and browse to see if it works…

Nope, some SQL access error this time:

Cannot open database “Subtext2.1″ requested by the login. The login failed.
Login failed for user ‘IIS APPPOOL\DefaultAppPool’.

This is quite straightforward. The IIS running user doesn’t have access to the database file. But there is a catch. The “Web.config” file that is in the solution references a “.mdf” file. For some reason, I was unable to make it run with an instance database. I was also unable to find any answer about that yet. You’ll have to head to SQL Management Studio and create a database for Subtext to use.

Now, that’s not the only thing. Subtext will not work well with the new IIS 7 integrated pipeline. You’ll have to run Subtext in “classic” mode. Do this by simply changing the AppPool that runs Subtext.

VS9SubtextConfiguration

One this is done, you should be able to access the install page of Subtext (just hit the root folder in IE and the wizard will take you trough.

That’s it!

PS: there is a question on StackOverflow about IIS security model, DefaultAppPool user, etc. I just posed some questions, but no one seems to be able to answer.

Windows 7, SQL Server 2008 and Subtext

As I’m now back to Java for work and miss .NET, I’m going to mess a bit with open source projects. The one I picked is Subtext, a blog engine written in ASP.NET. I’m not an ASP.NET guru, so messing around (and maybe even help, who knows?) with a project like this will likely get me some experience.

I just installed Windows 7 RC on my PC, so I’m starting from scratch here. I installed Visual Studio 2008 Standard edition, then SQL Server 2008 Express. The code that I checked out from Subtext’s SVN doesn’t build with that configuration, it needs some tweaking.

As I have been struggling to get the project to build, I writing a small post to summarize all the issues that I had and how I solved them.

SQLDMO

The first error that you will encounter when trying to build Subtext out of the SVN is this one:

Type or namespace name SQLDMO could not be found

SQLDMO is deprecated in SQL Server 2008. However, as pointed in this forum thread, there is a backward compatibility pack.

References in UnitTests.Subtext

In UnitTests.Subtext project, some of the references are broken.

VS9UnitTestsSubtext

As stated here, these DLLs from SQL Server 2005 have been removed from SQL Server 2008.

Delete the references to:

The add the references back using “Add Reference…” dialog.

However, this is not sufficient. The project will not build and still complain:

The type ‘Microsoft.SqlServer.Management.Sdk.Sfc.IDmfFacet’ is defined in an assembly that is not referenced. You must add a reference to assembly ‘Microsoft.SqlServer.Management.Sdk.Sfc, Version=10.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91′.

Again, using the “Add Reference…” dialog, add a reference to Microsoft.SqlServer.Management.Sdk.Sfc.

VS9UnitTestsSubtextReference

This will leave you with only two Warnings about references to CookComputing.XmlRpcV2 and Microsoft.ApplicationBlocks.Data.

VS9UnitTestsSubtextWarnings

However, in this state you can build the project completely. If you delete the references in the project, it will not build anymore, for a reason that I still have to identify…

And Now?

Well, technically you can build and deploy now. I’ll talk about that in coming post.

Enumerable.Empty with null Coalescing Operation

Today I read Eric Lipert’s blog latest entry. It is about the semantic difference between null and empty. An easy example is with Collections. An empty Collection is not the same as an non-existing (ie. null) Collection.

However, as mentioned, how may times did you wish that foreach statement would work on an null Collection? How convenient would it be!

So far, what I was doing was:

foreach (var item in list ?? new List<SomeType>())
{
    //Do Stuff...
}

But something nicer exists: Enumerable.Empty. Using that, the code becomes:

foreach (var item in list ?? Enumerable.Empty<SomeType>())
{
    //Do Stuff
}

It’s actually longer, but it reads easier. The intent is quite obvious, I bet you can show this to someone who doesn’t know about the null coalescing operator and he would get what it does! Unfortunately I’m now working on Java and my colleagues would burn me alive if they knew I secretly pledged allegiance to .NET…