title: How the heck can you edit valid XML in a webpage?
body: <p>I've got to get a quick and dirty configuration editor up and running.  The flow goes something like this:</p>
  
  <p>configuration (POCOs on server) are serialized to XML.<br>
  The XML is well formed at this point. The configuration is sent to the web server in XElements.<br>
  On the web server, the XML (Yes, ALL OF IT) is dumped into a textarea for editing.<br>
  The user edits the XML directly in the webpage and clicks Submit.<br>
  In the response, I retrieve the altered text of the XML configuration.  At this point, ALL escapes have been reverted by the process of displaying them in a webpage.<br>
  I attempt to load the string into an XML object (XmlElement, XElement, whatever).  KABOOM.</p>
  
  <p>The problem is that serialization escapes attribute strings, but this is lost in translation along the way.  </p>
  
  <p>For example, let's say I have an object that has a regex.  Here's the configuration as it comes to the web server:</p>
  
  <pre><code>&lt;Configuration&gt;
    &lt;Validator Expression="[^&amp;lt;]" /&gt;
  &lt;/Configuration&gt;
  </code></pre>
  
  <p>So, I put this into a textarea, where it looks like this to the user:</p>
  
  <pre><code>&lt;Configuration&gt;
    &lt;Validator Expression="[^&lt;]" /&gt;
  &lt;/Configuration&gt;
  </code></pre>
  
  <p>So the user makes a slight modification and submits the changes back.  On the web server, the response string looks like:</p>
  
  <pre><code>&lt;Configuration&gt;
    &lt;Validator Expression="[^&lt;]" /&gt;
    &lt;Validator Expression="[^&amp;]" /&gt;
  &lt;/Configuration&gt;
  </code></pre>
  
  <p>So, the user added another validator thingie, and now BOTH have attributes with illegal characters.  If I try to load this into any XML object, it throws an exception because &lt; and &amp; are not valid within a text string.  I CANNOT CANNOT CANNOT CANNOT use any kind of encoding function, as it encodes the entire bloody thing:</p>
  
  <p>var result = Server.HttpEncode(editedConfig);</p>
  
  <p>results in </p>
  
  <pre><code>&amp;lt;Configuration&amp;gt;
    &amp;lt;Validator Expression="[^&amp;lt;]" /&amp;gt;
    &amp;lt;Validator Expression="[^&amp;amp;]" /&amp;gt;
  &amp;lt;/Configuration&amp;gt;
  </code></pre>
  
  <p>This is NOT valid XML.  If I try to load this into an XML element of any kind I will be hit by a falling anvil.  I don't like falling anvils. </p>
  
  <p>SO, the question remains...  Is the ONLY way I can get this string XML ready for parsing into an XML object is by using regex replaces?  Is there any way to "turn off constraints" when I load?  How do you get around this???</p>
  
  <hr>
  
  <p>One last response and then wiki-izing this, as I don't think there is a valid answer.</p>
  
  <p>The XML I place in the textarea IS valid, escaped XML.  The process of 1) putting it in the text area 2) sending it to the client 3) displaying it to the client 4) submitting the form it's in 5) sending it back to the server and 6) retrieving the value from the form REMOVES ANY AND ALL ESCAPES.  </p>
  
  <p>Let me say this again:  I'M not un-escaping ANYTHING.  Just displaying it in the browser does this!</p>
  
  <p>Things to mull over:  Is there a way to prevent this un-escaping from happening in the first place?  Is there a way to take almost-valid XML and "clean" it in a safe manner?</p>
  
  <hr>
  
  <p>This question now has a bounty on it.  To collect the bounty, you demonstrate how to edit VALID XML in a browser window WITHOUT a 3rd party/open source tool that doesn't require me to use regex to escape attribute values manually, that doesn't require users to escape their attributes, and that doesn't fail when roundtripping (&amp;amp;amp;amp;amp;etc;)</p>