我正在使用 XDocument 来切换 xml 文档中的值。在新值中,我需要使用字符 '&'(与号),但在 XDocument.save() 之后,xml&
代替了!
我尝试使用编码和其他东西……没有任何效果
我正在使用 XDocument 来切换 xml 文档中的值。在新值中,我需要使用字符 '&'(与号),但在 XDocument.save() 之后,xml&
代替了!
我尝试使用编码和其他东西……没有任何效果
XDocument 正在做它应该做的事情。
&
是无效的 XML。(这是一个未完成的角色/实体参考)
&
在 XML 中表示“实体的开始”,因此如果要包含&
as 数据,则必须将其表示为实体 - &
(或在 CDATA 块中使用它)。
您所描述的是正常行为,否则 XML 会中断。
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
有两种选择。要么确保对 XML 文档中的所有内容进行正确的 XML 编码/解码。请记住,HTML 和 XML 编码/解码略有不同。
选项二是对 xml 中可能包含无效元素的任何内容使用 base64 编码。