title: Force XML character entities into XmlDocument
body: <p>I have some XML that looks like this:</p>
  
  <pre><code>&lt;abc x="{"&gt;&lt;/abc&gt;
  </code></pre>
  
  <p>I want to force XmlDocument to use the XML character entities of the brackets, ie:</p>
  
  <pre><code>&lt;abc x="&amp;#123;"&gt;&lt;/abc&gt;
  </code></pre>
  
  <p>MSDN says this: </p>
  
  <blockquote>
    <p>In order to assign an attribute value
    that contains entity references, the
    user must create an XmlAttribute node
    plus any XmlText and
    XmlEntityReference nodes, build the
    appropriate subtree and use
    SetAttributeNode to assign it as the
    value of an attribute.</p>
  </blockquote>
  
  <p><a href="http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.createentityreference.aspx" rel="nofollow noreferrer">CreateEntityReference</a> sounded promising, so I tried this:</p>
  
  <pre><code>XmlDocument doc = new XmlDocument();
  doc.LoadXml("&lt;abc /&gt;");
  XmlAttribute x = doc.CreateAttribute("x");
  x.AppendChild(doc.CreateEntityReference("#123"));
  doc.DocumentElement.Attributes.Append(x);
  </code></pre>
  
  <p>And I get the exception <code>Cannot create an 'EntityReference' node with a name starting with '#'.</code></p>
  
  <p>Any reason why CreateEntityReference doesn't like the '#' - and more importantly how can I get the character entity into XmlDocument's XML?  Is it even possible?  I'm hoping to avoid string manipulation of the OuterXml...</p>