Note that there are some explanatory texts on larger screens.

plurals
  1. POEncode double quotes inside XML element using LINQ to XML
    primarykey
    data
    text
    <p>I'm parsing a string of XML into an XDocument that looks like this (using XDocument.Parse)</p> <pre><code>&lt;Root&gt; &lt;Item&gt;Here is &amp;quot;Some text&amp;quot;&lt;/Item&gt; &lt;/Root&gt; </code></pre> <p>Then I manipulate the XML a bit, and I want to send it back out as a string, just like it came in</p> <pre><code>&lt;Root&gt; &lt;Item&gt;Here is &amp;quot;Some text&amp;quot;&lt;/Item&gt; &lt;NewItem&gt;Another item&lt;/NewItem&gt; &lt;/Root&gt; </code></pre> <p>However, what I am getting is</p> <pre><code>&lt;Root&gt; &lt;Item&gt;Here is \"Some text\"&lt;/Item&gt; &lt;NewItem&gt;Another item&lt;/NewItem&gt; &lt;/Root&gt; </code></pre> <p>Notice how the double quotes are now escaped instead of encoded?</p> <p>This happens whether I use </p> <pre><code>ToString(SaveOptions.DisableFormatting); </code></pre> <p>or</p> <pre><code>var stringWriter = new System.IO.StringWriter(); xDoc.Save(stringWriter, SaveOptions.DisableFormatting); var newXml = stringWriter.GetStringBuilder().ToString(); </code></pre> <p>How can I have the double quotes come out as <code>&amp;quot;</code> and not <code>\"</code>?</p> <p><strong>UPDATE</strong>: Maybe this can explain it better:</p> <pre><code>var origXml = "&lt;Root&gt;&lt;Item&gt;Here is \"Some text&amp;quot;&lt;/Item&gt;&lt;/Root&gt;"; Console.WriteLine(origXml); var xmlDoc = System.Xml.Linq.XDocument.Parse(origXml); var modifiedXml = xmlDoc.ToString(System.Xml.Linq.SaveOptions.DisableFormatting); Console.WriteLine(modifiedXml); </code></pre> <p>the output I get from this is:</p> <pre><code>&lt;Root&gt;&lt;Item&gt;Here is "Some text&amp;quot;&lt;/Item&gt;&lt;/Root&gt; &lt;Root&gt;&lt;Item&gt;Here is "Some text"&lt;/Item&gt;&lt;/Root&gt; </code></pre> <p>I want the output to be:</p> <pre><code>&lt;Root&gt;&lt;Item&gt;Here is "Some text&amp;quot;&lt;/Item&gt;&lt;/Root&gt; &lt;Root&gt;&lt;Item&gt;Here is "Some text&amp;quot;&lt;/Item&gt;&lt;/Root&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload