1

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

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
Ron Gross
  • 1,474
  • 5
  • 19
  • 34

4 Answers4

4

XDocument is doing exactly what it's supposed to do.

& is invalid XML. (it's an unfinished character/entity reference)

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • i must have this sign, the xml is an app.config that has a connection string to a db, and i need to change it via code – Ron Gross Dec 11 '11 at 14:18
  • 5
    No; you must _not_ have this sign, because that would give a parse error. The escaped representation (`&`) _is_ correct and will work fine (assuming you're using an actual XML parser) – SLaks Dec 11 '11 at 14:19
  • the xml is an app.config file inside c# project, and it is not working with & – Ron Gross Dec 11 '11 at 14:21
  • 2
    What code do you have that is reading the app.config; I think *there* (the reading) is where the error is here – Marc Gravell Dec 11 '11 at 14:31
  • The issue here is that, for example, Entity Framework's parser parses the connection string just fine even with the & in the attribute value. – Roberto Bonini Sep 02 '14 at 11:09
3

& 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.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • the & is inside an attribute value... and is inside connectionString so i must have it... any ideas? – Ron Gross Dec 11 '11 at 14:19
  • 1
    Being inside an attribute value just means that using a CDATA block is not an option. Using `&` is thus the **only** to express an ampersand character as part of the data. If the software using the connection string is trying to use `&` to connect to the database instead of `&` then its XML parser is broken and *that* is what you need to fix. – Quentin Dec 11 '11 at 14:19
  • The write takes the & and encodes it, the reader takes the encoded version and decodes it. If you are getting the encoded version, some one somewhere hasn't decoded it. If your config file has an unescaped & in an attribute value, you can't view it as an xml document.it isn't xml. Just looks like it if you squint. – Tony Hopkinson Dec 11 '11 at 15:56
0

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.

ahybertz
  • 514
  • 4
  • 9
0

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

Community
  • 1
  • 1
Michael Kay
  • 156,231
  • 11
  • 92
  • 164