I am using XDocument to switch a value in an xml document.
In the new value I need to use the character '&' (ampersand)
but after XDocument.save() the xml has &
instead!
I tried using encoding and stuff… nothing worked
I am using XDocument to switch a value in an xml document.
In the new value I need to use the character '&' (ampersand)
but after XDocument.save() the xml has &
instead!
I tried using encoding and stuff… nothing worked
XDocument is doing exactly what it's supposed to do.
&
is invalid XML. (it's an unfinished character/entity reference)
&
means "Start of an entity" in XML so if you want to include an &
as data you must express it as an entity — &
(or use it in a CDATA block).
What you describe is normal behaviour and the XML would break otherwise.
There are two options. Either to ensure proper XML encoding/decoding of all your content in the XML document. Remember that HTML and XML encoding/decoding is slightly different.
Option two is to use base64 encoding on whatever content in the xml that might contain invalid elements.
Is your output file app.config supposed to be an XML file?
If it is, then the & must be escaped as &
.
If it isn't, then you should be using the text output method instead of the xml output method: use <xsl:output method='text'/>
.
PS: this question appears to be a duplicate of How can I add an ampersand for a value in a ASP.net/C# app config file value