0

I would like to transform a string like this

"¿Cómo puede hacerse esto?"

Into something that can be used within an XML resx file like this:

"¿Cómo puede hacerse esto?"

I have tried HttpUtility.UrlEncode and WebUtility.HtmlEncode without any success. I have searched online and can't seem to find the right solution.

I think I may need to put the string into a character array and iterate through each character and work out which one needs to be transformed.

Can anyone help me please?

Richard Ev
  • 52,939
  • 59
  • 191
  • 278
Alex
  • 3
  • 4

1 Answers1

0

you can try to use HttpUtility.HtmlEncode if you are using an asp.net project or something like the follwong snippet code if not

        string str ="¿Cómo puede hacerse esto?";
        string newMStr = string.Empty; 

        foreach (var ch in str)
        {
            int chCode = Convert.ToInt16(ch); 
            if (chCode>127)
            {
                newMStr += string.Format("&#{0};", chCode);
            }
            else
            {
                newMStr += ch;    
            }


        }


        //Print  NewMstr
BRAHIM Kamel
  • 13,492
  • 1
  • 36
  • 47
  • Thanks - I have tried the above example but I get an overflow exception on the Convert.ToInt16, when I change it to Convert.Int32 I end up with � which doesn't seem right. – Alex Dec 11 '13 at 12:52
  • can you debug it and show me at wich character please – BRAHIM Kamel Dec 11 '13 at 12:55
  • the code works perfectly for me for the example you have provided – BRAHIM Kamel Dec 11 '13 at 12:57
  • I have sorted it now - your code is now working fine for me. I had the detectEncodingFromByteOrderMarks set to true (content was coming from CSV file). Setting this property to false resolved the issue. – Alex Dec 11 '13 at 13:25