views:

334

answers:

3

After extracting a piece of text in my application, I might end up with a string like this:

"More kitchen supplies for the people"

Which in plain text would be:

"More kitchen supplies for the people"

Is there a component/method in .NET I can use to "process" the string into its plain text equivalent?

I'm able to assume regular ascii text in general, no odd unicode or strange alphabets. It just seems that normal signs like ', ", | etc. are provided as character codes.

EDIT: I think I should note that this is about .NET for Windows desktop development. Appearantly there are methods to do this for ASP.NET only, and I didn't realize that simple operations like this could be limited to specific .NET platforms.

+8  A: 

The System.Web.HttpUtility.HtmlDecode method can handle this:

(powershell v2 example)

ps> add-type -an system.web
ps> [system.web.httputility]::HtmlDecode(""")
"
ps>

Hope this helps,

-Oisin

x0n
I'm not sure why, but "HttpUtility" is not present in my System.Web namespace. Looking at the online help, it should be part of the .NET library, but the example code only shows examples for ASP.NET.
sharkin
System.Web.HttpUtility is a class in the .NET 2.0 System.Web assembly. I'm pretty sure it's in 1.1 too. What version are you usuing?
x0n
I'm using .NET 3.5 SP1. The System.Web assembly is version 2.0 according to the references dialog. I have only three classes in System.Web: AspNetHostingPermission, AspNetHostingPermissionAttribute and AspNetHostingPermissionLevel. Despite intellisense I tried to instantiate HttpUtility, but I get: error CS0234: The type or namespace name 'HttpUtility' does not exist in the namespace 'System.Web'.
sharkin
You don't instantantiate it - it's a static class.System.Web.HttpUtility.HtmlDecode("my text ...")
x0n
If you only have 3 classes in system.web, something is very very wrong.
x0n
I suspected it to be static, but I get errors no matter how I use it ofcourse. I guess I'll have to post a new question about my problem.
sharkin
Didn't know I hade to add a reference to my project for System.Web. http://stackoverflow.com/questions/1531906/c-net-help-i-have-only-three-classes-in-my-system-web-namespace
sharkin
lol! that would explain it - well I learned something too. I didnt know there were some system.web.* types defined OUTSIDE of system.web assembly. :)
x0n
A: 

If you know the codes are ASCII you can use the following to convert each little &x22 number.

public char Convert(string data) {
  data = data.SubString(1);  // Lose the &
  var num = Int32.Parse(data, NumberStyles.Hex | NumberStyles.AllowHexSpecifier);
  var chars = Encoding.ASCII.GetChars(new byte[] { (byte)num });
  return chars[0];
}
JaredPar
A: 

I get an error

Error 1 The type name 'HtmlDecode' does not exist in the type 'System.Web.HttpUtility'

Ingmar Eidem