Tag Archives: C#
Reading a RSS feed with the C# XmlReader
Posted on 01. Feb, 2010 by Wim Haanstra.
Today I needed to parse a LARGE (1.9GB) XML file and extract some information from it. Loading it in a XmlDocument (my favorite .NET way of handling XML) wasn’t really a possibility, because that would cause Out of Memory errors the minute I would try it.
I am using a XmlReader for this and I noticed that there arent really many usable examples on the internet about this, so here is just a small example I created to show how you could read a RSS feed.
Sample after the break… (more…)
Continue ReadingResizing an image in C#
Posted on 26. Jan, 2010 by Wim Haanstra.
For a project I am working on, I was looking for a good way to resize images in a few different ways. I have done image resizing before, but it really feels that I am re-inventing the wheel every time I do this. So now I decided to write a nice static method which can resize an Image to certain dimensions and outputs an Image object again.
The method requires 3 parameters:
1. An image object. I hope you know how to load a file in an Image object (hint: FromFile).
2. A Resolution object, being nothing more than a small class I created, which also contains the width and the height.
3. The mode you want to use for resizing the image. I explained the modes in the enum listed below.
It probably isnt the neatest code you’ve ever seen, so if you have any comments, please let me know. Code and more explanation after the break… (more…)
Continue ReadingApplication.Lock() & Current session count
Posted on 02. Jul, 2008 by Wim Haanstra.
I have been looking around the net to find the best way to keep track of the total number of concurrent sessions. There is one piece of code, that is posted on a lot of websites.
The code that I find mostly is the following (I editted a bit, to match my coding style):
Global.asax
[csharp]
protected void Application_Start(object sender, EventArgs e)
{
Application["SessionCount"] = 0;
}
protected void Session_Start(object sender, EventArgs e)
{
Application.Lock();
int sessionCount = 0;
if (Int32.TryParse(Application["SessionCount"].ToString(), out sessionCount))
sessionCount++;
Application["SessionCount"] = sessionCount;
Application.UnLock();
}
protected void Session_End(object sender, EventArgs e)
{
Application.Lock();
int sessionCount = 0;
if (Int32.TryParse(Application["SessionCount"].ToString(), out sessionCount))
sessionCount–;
Application["SessionCount"] = sessionCount;
Application.UnLock();
}
[/csharp]
The thing I am wondering about is, what happens when the site gets a lot of load. Does locking the Application object make the site slower? Because I noticed that when you lock the Application object, no pages get served to users.
What happens with multi-threaded web applications? Are all threads blocked during the lock?
I don’t know the answer yet, but I think this is a nice thing to try to find out.
Continue ReadingConverting 70.000+ XML Documents
Posted on 02. Jul, 2008 by Wim Haanstra.
At work I have written a XML converter, which should (daily) convert over 70.000 XML documents to a different format. Depending on some attributes set in the XML file, a couple of values of elements should be extracted and inserted in the XML document again (but in a different location).
Please don’t ask me why I need this, but it is a terrible solution for something that would sound so simple.
The only way to solve this problem (which I saw) was opening each individual file, load them in an XmlDocument object and track down the elements and attributes I needed using xPath.
Only halfway the script the memory consumption of the tool goes through the roof (around ~1.4GB), at least on our Windows 2003 server, where the application is running.
When I run that application on my Windows Vista x64 machine, there is no problem at all and the memory consumption is at ~20MB.
I tried finding memory leaks in my application, by using a couple of different profile applications, but they didn’t detect any large memory leaks, that could cause these problems.
What the heck is going on?
*sigh*: somehow the server puts all the 70.000 XML files in 5 files, and that way it creates 5 files of around 800MB. I disabled that script and now the memory consumption on the server is also back to normal.
Continue ReadingVraagOns.nl, pretty cool techniques
Posted on 01. Jul, 2008 by Wim Haanstra.
Since a couple of weeks I working on a private project, called VraagOns.nl which is based on an idea about a useful website I have. Currently the project is in a concept fase and I am still working out different details, but it is getting along nicely.

I won’t really reveal any details about the project, but I wanted to tell you guys what this project is based on and which techniques I (try) to use. Because I develop on my own, I can use any technique I want and that makes the possibilities unlimited in this project.
The techniques I use are (in a nutshell):
- .NET 3.5
- LiNQ
- AJAX
- Lucene.NET
None of these things are final yet, but I just wanted to take them for a test-drive. I have used .NET 3.5, AJAX and Lucene.NET previously, but I am currently still stress-testing LiNQ to make sure it can handle the number of hits I am hoping for
.
Lucene.NET is also interesting I also use this at work, where I created a nice library for it, which makes it very easy to use. But just to be on the safe side, I created another implementation of this, mainly to avoid ‘intellectual property’ problems.
The use of AJAX in this project is limited, I really like AJAX, but using too much of it, can really take down the usability of your website. Think about browsing history, direct-linking to a certain page, etc.
All of this is hosted on a Windows based server, provided by my good friend at Qsoft.be, so give him an applause for that!
Continue ReadingBlogEngine.net
Posted on 10. May, 2008 by Wim Haanstra.
I am currently in the progress of moving to a .NET based blog. Currently I am looking at BlogEngine.NET but it misses some vital features.
For example, I like the option I added to this blog to attach files to a post and have a nice file list beneath my post listing the files. There is no real option in BlogEngine.NET to do this, so I added this feature in the latest release of BE (1.3.1.0).
Here’s a screenie of the admin screen and a sample post:

If you are a BlogEngine.NET user and you would like to attach files to your post, just gimme a sign and I will see if I can supply some sources. Maybe it’s not the nicest solution, but I requested this option on their forums too.
Continue ReadingThings that keep me occupied this week
Posted on 07. May, 2008 by Wim Haanstra.
This week will be a very busy week… I just got back from a 10 day break from work, but things never stop piling up…. it seems.
Next to work I got a couple of projects that keep me busy at home too. There’s computer stuff:
Getting my server to run fluintly on MacOS-X
Because that stupid HyperV kept crashing on me lately, I got the idea to actually install MacOS-X on my server, to see how it performs. My server is a pretty hefty beast, running a nice Quadcore Q9450 CPU and 12 GB of RAM. I got it installed yesterday and it seems pretty stable, accept some ‘minor’ issues (will post that later on).
If you want to read more of my wining, click ‘more’…. (more…)
Continue ReadingXmlDocument fluent interface
Posted on 27. Feb, 2008 by Wim Haanstra.
First I want to make clear, this is not my code, but it works brilliantly so I wanted to share it with you anyway.
Check out the following link:
improve.dk – XmlDocument fluent interface.
You can download the class mentioned in the post here. At the bottom of the post.
This is a bit of sample code.
1 2 3 4 | XmlOutput xo = new XmlOutput() .XmlDeclaration() .Node("Error").InnerText("Exception occured: " + ex.Message); return xo.GetXmlDocument(); |
XNA: VMWare and XNA GS are no friends
Posted on 23. Jan, 2008 by Wim Haanstra.
So, I found the solution for my problem with XNA GS 2.0.
It seems that XNA GS 2.0 could not handle the virtual adapters installed on my development system. I deinstalled VMWare and all was fine!
If you want to know what my problem was, read it here.
Continue ReadingXNA: Cannot connect my PC to my Xbox
Posted on 23. Jan, 2008 by Wim Haanstra.
I have an issue with XNA Game Studio Connect. Yesterday I purchased my XNA membership subscription and installed it for the first time on my Xbox hard drive. I started it up and it gave me a key. I entered this key, together with a name in my XNA Game Studio Device Center and tried to connect and that failed.
I tried several different keys just to make sure I got the whole key right (altough I was reading the key from a 720P screen, I must say the I character is pretty confusing
).
(more…)

