+1  A: 

You can for example encode the string as UTF-8, then encode the data using base-64:

string encoded = Convert.ToBase64String(Encoding.UTF8.GetBytes(theString));

This gives you a string without any special characters.

Deserialising is the reverse:

string theString = Encoding.UTF8.GetString(Convert.FromBase64String(encoded));
Guffa
conversion should be done before serializing and deserializing?
@starz: The conversion is the serialisation. You can put the encoded string straight into the XML.
Guffa
HOw abt the deserialization, should this decoding done after dersializing or the decoded shoud be passed as input for Deserialization
@starz: Same there, you can pull the value straight from the XML, convert it, and you have the original string.
Guffa
string str = 23 + "AC" + (char)1; XmlSerializer serializer = new XmlSerializer(typeof(String)); string encoding = Convert.ToBase64String(Encoding.UTF8.GetBytes(str)); StringWriter sw = new StringWriter(); serializer.Serialize(sw, encoding); System.Console.WriteLine("String encoded to XML = \n{0} \n", sw.ToString()); StringReader sr = new StringReader(sw.ToString()); String s2 = (String)serializer.Deserialize(sr); string ecodedStr = Encoding.UTF8.GetString(Convert.FromBase64String(s2));
check this once if i have done in the correct order
@starz: Yes, that works.
Guffa
Thanks, but the result of serialization is some thing like "String encoded to XML = <?xml version="1.0" encoding="utf-16"?> <string>MjNBQwE=</string> " and the result of deserialization is "String decoded from XML = 23AC". My question is, before when i dint use encoding the serialized data was same inputString appended with escapse chars as "String encoded to XML = <?xml version="1.0" encoding="utf-16"?> <string>23AC</string> "Does thhis mean are we losing some thing using this encoding
When I try the code I don't lose any character. If you lose any character, it's in the decoding from UTF-8, as the encoded string is correct. In that case the decoder doesn't allow the control characters, and your only option would be to get the character codes of the characters in the string. Encoding: `string encoding = String.Concat(str.Select(c => ((int)c).ToString("x4")).ToArray());` and decoding: `string ecodedStr = new String(Regex.Matches(s2, "(.{4})").Cast<Match>().Select(m => (char)Convert.ToInt16(m.Value, 16)).ToArray());`.
Guffa
A: 

How about using XmlWriter to write with and XmlTextReader to read with? Example:

var sb = new StringBuilder();

// set the encoding here: you may need to try different encoding types...
var settings = new XmlWriterSettings{ Encoding = System.Text.Encoding.UTF8 };

using (var writer = XmlWriter.Create(sb, settings))
{
    string str = 23 + "AC"+(char)1;
    XmlSerializer serializer = new XmlSerializer(typeof(String));
    serializer.Serialize(writer, str);
    System.Console.WriteLine("String encoded to XML = \n{0} \n", sw.ToString());

    // more code to handle read the values with XmlTextReader, etc etc etc......    
}
code4life