How to encode special characters in XML document?

H

Hardy Wang

Hi,
I have a XML document, loaded by

XmlDocument xDoc = new XmlDocument();
xDoc.Load("my.xml");
xPath = xmlKeyName;
XmlNode xNode = xDoc.SelectSingleNode(xPath);

Then I will assign some value to one certain key by:
xNode.InnerXml = "<displayName>" + Name + "</displayName>";

If there are some special characters in my string "Name", such as "&",
"<" or ">", program will raise error. Is there a way I can use to encode all
special characters to eliminate potential problems?
 
T

Tarakeshwar L

Replace '<' with '&lt' and replace '>' with '&gt'. For '&' you could try
using the escape sequence '\&'

Regards
Tarakeshwar
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top