Of course you know that XML denotes element names with greater-than and less-than symbols, such as:

<name>value</name>

Therefore, to avoid confusing the XML parser, the greater/less symbols (and the ampersand, an HTML special character) must be encoded.

For example, given the following string:

string text = "Here is a <Test String> & more";

To encode the string to an XML value, use the HttpUtility.HtmlEncode method:

string xmlValue = HttpUtility.HtmlEncode( text );

The HttpUtility class is found in the System.Web namespace.  Note that you may have to add a System.Web reference to your Windows Forms or Console project.

Given the example above, xmlValue will be:

Here is a &lt;Test String&gt; &amp; more

The XMLWriter class will encode values for you automatically, but this tip is handy if you are generating your own XML text. 

Note that if you need to encode text that will be used as an XML element name (rather than its value), you can use the XmlConvert.EncodeLocalName method found in the System.Xml namespace. 

It’s a mystery to me why the .NET designers did not include the HtmlEncode capability in the XmlConvert class.  The ideal place would have been the overloaded XmlConvert.ToString method, which accepts just about every .NET native type except a string.

Share and Enjoy:
  • Digg
  • Twitter
  • Facebook
  • Reddit
  • StumbleUpon
  • LinkedIn
  • Google Bookmarks
  • Slashdot