XML Encoding

The following code will XML-encode the specified string. You can convert code between C# and VB.NET at this website.

using System.IO;
using System.Xml;

StringWriter s = new StringWriter();
XmlTextWriter w = new XmlTextWriter(s);
w.WriteString(inputText);
string result = s.ToString();

Alternate Method

public static string XmlEncode(string input)
{
    input = input.Replace("&", "&");
    input = input.Replace("\"", """);
    input = input.Replace("'", "'");
    input = input.Replace("<", "&lt;");
    input = input.Replace(">", "&gt;");
    return input;
}