This is off the cuff, so pardon any errors. I am sure it can be improved and
may have some bugs (;->). As it uses a switch (state machine), it should be
faster than a dual loop (character array and array of chars to look for).
The &#{number}; should be fine as escaped XML.
public string XmlEncode(string nonXmlText)
{
StringBuilder builder = new StringBuilder();
chars[] originalChars = nonXmlText.ToCharArray();
for(int i=0;i<originalChars.Length;i++)
{
switch((byte)originalChars[i])
{
case 34:
case 38:
case 39:
case 60:
case 61:
case 62:
builder.Append("&#");
builder.Append(originalChars(i);
builder.Append(";");
break;
default:
builder.Append(originalChars[i]);
break;
}
}
return builder.ToString();
}
--
Gregory A. Beamer
*************************************************
Think Outside the Box!
*************************************************
"Mantorok" <ma******@mantorok.com> wrote in message
news:e5**********@newsfeed.th.ifl.net...
Hi all
I have a plain text string, sometimes the string will contain special
characters, how can I encode this string in xml format?
Thanks
Kev