2
StringBuilder request = new StringBuilder();
request.Append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");

byte[] msg;
string decoded;

msg=System.Text.Encoding.UTF8.GetBytes(request.ToString());
decoded = System.Text.Encoding.UTF8.GetString(msg);

decoded is <?xml version=\"1.0\" encoding=\"UTF-8\"?>

the result has \" My question is how can I encode the message with quotes and still get string without \" after decdoing If I use AppendLine that the result also has \r\n

Preet Sangha
  • 64,563
  • 18
  • 145
  • 216
  • 1
    Where are you looking at the result? In Visual Studio Debug mode? If this is the case rest assured => there are no `\"` in the actual string. It's the debugger representation of strings. This being said, using a StringBuilder to manipulate XML is probably one of the worst things you could do. XML should be manipulated only with XML parsers. – Darin Dimitrov Jun 13 '12 at 08:42
  • Thanks and Yes I was looking in the debugger, As this is just a single message I will send I wasnt XML parser..But I will have look into it thanks – user1178514 Jun 13 '12 at 08:56

2 Answers2

7

Are you looking at the string in the debugger? If so it will show the string as if it were a string literal in C#. The \ are not really there. You can esily verify that if you output the string somewhere.

Joey
  • 344,408
  • 85
  • 689
  • 683
0

@joey's answer is the correct one - but since XML supports single quotes you can use this instead - which I find cleaner to read:

request.Append("<?xml version='1.0' encoding='UTF-8'"?>");
Preet Sangha
  • 64,563
  • 18
  • 145
  • 216