title: Best way to encode text data for XML
body: <p>I was looking for a generic method in .Net to encode a string for use in an Xml element or attribute, and was surprised when I didn't immediately find one.  So, before I go too much further, could I just be missing the built-in function?  </p>
  
  <p>Assuming for a moment that it really doesn't exist, I'm putting together my own generic <code>EncodeForXml(string data)</code> method, and I'm thinking about the best way to do this.  </p>
  
  <p>The data I'm using that prompted this whole thing could contain bad characters like &amp;, &lt;, &quot;, etc.  It could also contains on occasion the properly escaped entities: &amp;amp;, &amp;lt;, and &amp;quot;, which means just using a CDATA section may not be the best idea.  That seems kinda klunky anyay; I'd much rather end up with a nice string value that can be used directly in the xml.</p>
  
  <p>I've used a regular expression in the past to just catch bad ampersands, and I'm thinking of using it to catch them in this case as well as the first step, and then doing a simple replace for other characters.  </p>
  
  <p>So, could this be optimized further without making it too complex, and is there anything I'm missing? :</p>
  
  <pre class="lang-vb prettyprint-override"><code>Function EncodeForXml(ByVal data As String) As String
      Static badAmpersand As new Regex("&amp;(?![a-zA-Z]{2,6};|#[0-9]{2,4};)")
  
      data = badAmpersand.Replace(data, "&amp;amp;")
  
      return data.Replace("&lt;", "&amp;lt;").Replace("""", "&amp;quot;").Replace("&gt;", "gt;")
  End Function
  </code></pre>
  
  <p><em>Sorry for all you C# -only folks-- I don't really care which language I use, but I wanted to make the Regex static and you can't do that in C# without declaring it outside the method, so this will be VB.Net</em></p>
  
  <p>Finally, we're still on .Net 2.0 where I work, but if someone could take the final product and turn it into an extension method for the string class, that'd be pretty cool too.</p>
  
  <p><strong>Update</strong> The first few responses indicate that .Net does indeed have built-in ways of doing this.  But now that I've started, I kind of want to finish my EncodeForXml() method just for the fun of it, so I'm still looking for ideas for improvement.  Notably: a more complete list of characters that should be encoded as entities (perhaps stored in a list/map), and something that gets better performance than doing a .Replace() on immutable strings in serial.</p>