0
string x = "&Microsoft<?xml version=";
string y = System.Web.HttpUtility.HtmlDecode(x);
Console.WriteLine(y);
Console.ReadLine();

Now the y value is &Microsoft<?xml version=

In this case the web.httputility.htmldecode not working

I am expecting result is &Microsoft<?xml version=

Please reply if there is answer for this.

  • 2
    The input string is in an invalid format/encoding, so it might be tricky to define a correct return value from that method. – Progman Aug 09 '20 at 08:30
  • 1
    Where is x coming from? It should actually be `&Microsoft…` to be valid as the & character masks the start of a HTML entity and must end with a ; character. Therefore if one wants to have the & itself it must be escaped by using & – ckuri Aug 09 '20 at 08:51
  • The value is coming from api. That api response is like that. – Saravanapandian Aug 09 '20 at 11:44

1 Answers1

1

The code looks good. Upon testing it on my local. I got what your expected result.

enter image description here

UPDATE
For .Net Core, it still the same code but rather remove the unnecessary '&' on the first character of the string. Because it is forbidden to use ampersand without any related url encoding version

eg: '&lt;' = '<'

You can check the code behind for .net core implementation of HtmlEncoder.cs

I hope it helps. Happy coding.

tontonsevilla
  • 2,649
  • 1
  • 11
  • 18