c# Checking if string is HtmlDecode I have this string that I am sometimes double encoding it because it is coming from two different fields. I have build this method that will do a string StringComparison. It seems complicated wanted to know if there is a better way to do something like this.

 class Program
    {
        static void Main(string[] args)
        {

            string str1 = "This string is already encode &amp;lt;0.1";

            Console.WriteLine(HTTPEncodeString(str1));


            string str2 = "This string needs to be encoded &lt;0.01";
            Console.WriteLine(HTTPEncodeString(str2));

        }

        public static string HTTPEncodeString(string source) 
        {
            if (EncodeText(source))
            {
                return source;


            }
            else
            {
                return HttpUtility.HtmlEncode(source);
            }


        }

        public static bool EncodeText(string val)
        {
            string decodedText = HttpUtility.HtmlDecode(val);
            string encodedText = HttpUtility.HtmlEncode(decodedText);

            return encodedText.Equals(val, StringComparison.OrdinalIgnoreCase);

        }
    }
This isn't a language I know, but there appears to be a problem with the logic.

Suppose I have a simple statement:


  In XML, ampersand can be represented using '&amp;amp;' or '&amp;#38;'


Is that an already-encoded string?  The logic here says that it is, but we see that's not the case - the HTML version of that would be


  In XML, ampersand can be represented using '&amp;amp;amp;' or '&amp;amp;#38;'


If it's hard to keep track in code, a better option might be a small class to hold a HTML-encoded string, so that we don't confuse these with content strings (or vice-versa, if HTML-encoded is the default in your target environment).

But TBH, I think that good variable naming should be enough to track which strings are which.