Javascript’s escape() in C# – a C# equivalent to the escape()-function

csharp_vs_javascriptThe .NET framework provides a large number of ways to encode HTML code and URLs. There exist, for example, Uri.EscapeDataString(), Uri.EscapeUriString(), System.Web.HttpUtility.UrlEncode(), System.Web.HttpUtility.UrlPathEncode(), System.Web.HttpUtility.HtmlEncode() or System.Web.HttpUtility.HtmlAttributeEncode().
However, if you are looking for equivalent functionality to Javascript’s escape(), so you will be disappointed by all of these aforementioned functions. None of these inherently with the .NET framework delivered functions is equivalent to the Javascript escape() function.  For better understanding, I made the following example:
The test text is in german, because the ‘ä’ is a nice character to show the problems. Translated to english it means: “Raffi’s annoying C# testcode”.

// Original source text
// Raffi’s ärgerlicher C# Teststring/Testcode.

// Javascript original (produced by escape())
// Raffi%27s%20%E4rgerlicher%20C%23%20Teststring/Testcode.

// Uri.EscapeDataString():
// Raffi’s%20%C3%A4rgerlicher%20C%23%20Teststring%2FTestcode.

// Uri.EscapeUriString():
// Raffi’s%20%C3%A4rgerlicher%20C#%20Teststring/Testcode.

// System.Web.HttpUtility.UrlEncode():
// Raffi%27s+%c3%a4rgerlicher+C%23+Teststring%2fTestcode.

// System.Web.HttpUtility.UrlPathEncode():
// Raffi’s%20%c3%a4rgerlicher%20C#%20Teststring/Testcode.

// System.Web.HttpUtility.HtmlEncode():
// Raffi's ärgerlicher C# Teststring/Testcode.

// System.Web.HttpUtility.HtmlAttributeEncode():
// Raffi's ärgerlicher C# Teststring/Testcode.

But I would not blog about it if I did not have a solution […]

Simple XML serialization in C#

C# XML SerialisierungToday I want to show you how to perform a XML serialization in C#. Serialization itself, means that you convert an object to another, transportable form.
Using XML serialization, it is possible, for example, to store an object of a class in the form of an XML file and restore it later.
This can make sense if you if you want to transfer an object, for example, via HTTP or  restore an object to its state after closing and reopening an application.
For our example, I created the following class “Blog”:

public class Blog
public string User { get; set; }
public string Pass { get; set; }
public string Subdomain { get; set; }
public string BaseUrl { get; set; }

The method used to serialize is as follows:

public […]

How to change the size of a textbox in C#

I admit, the title sounds almost trivial. But on the second sight, it is not so easy to change the size of a TextBox control in C#, because a TextBox does not have the AutoSize property. For other controls you can set the AutoSize property to false and then change the height (height-property). For the TextBox control this is unfortunately not the case.
Nevertheless you can set the height of a TextBox by using a small workaround. The trick is as follows:

textBoxTest.Multiline = true;
textBoxTest.MinimumSize = new Size(150, 24);
textBoxTest.Size = new Size(150, 24);
textBoxTest.Multiline = false;

First you have to set the multiline property to true. Then you can adjust, according to your mood, the minimum-size and the size property to change the size of the TextBox. When you’re done, you set the Multiline property back to false. The TextBox maintains the set size just yet. It’s that simple
Who still wonders, why you should […]