views:

545

answers:

2

Hi..i have a string that contains special character like (trademark sign etc). This string is set as an XML node value. But the special character is not rendered properly in XML, shows ??. This is how im using it.

String str=xxxx; //special character string

XmlNode node = new XmlNode();
node.InnerText = xxxx;

I tried HttpUtility.htmlEncode(xxxx) but it converts it into "&amp ;#8482;" so the output of xml is "&#8482"; instead of ™ I have also tried XmlConvert.ToString() and XmlConvert.EncodeName but it gives ??

+3  A: 

I strongly suspect that the problem is how you're viewing the XML. Have you made sure that whatever you're viewing it in is using the right encoding?

If you save the XML and then reload it and fetch the inner text as a string, does it have the right value? If so, where's the problem?

You shouldn't perform extra encoding yourself - let the XML APIs do their job.

Jon Skeet
A: 

I've had issues with some characters using htmlEncode() before, as well. Here's a good example of different ways to write your XML: Different Ways to Escape an XML String in C#. Check out #3 (System.Security.SecurityElement.Escape()) and #4 (System.Xml.XmlTextWriter), these are the methods I typically use.

JohnForDummies