XDocument保存,而不是仅仅&

时间:2022-11-11 23:34:54

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!

我正在使用XDocument切换xml文档中的值。在新的值中,我需要使用字符'&'(&'),但是在XDocument.save()之后,xml有&相反!

I tried using encoding and stuff… nothing worked

我试着用编码之类的东西…什么都没用

4 个解决方案

#1


4  

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

XDocument正在做它应该做的事。

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

&是无效的XML。(这是一个未完成的字符/实体引用)

#2


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

&在XML中表示“开始一个实体”,因此如果您想包含&作为数据,您必须将它表示为一个实体& & &;(或者在CDATA块中使用)。

What you describe is normal behaviour and the XML would break otherwise.

您所描述的是正常的行为,否则XML将会崩溃。

#3


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.

有两个选项。要么确保正确的XML编码/解码XML文档中的所有内容。请记住,HTML和XML编码/解码略有不同。

Option two is to use base64 encoding on whatever content in the xml that might contain invalid elements.

选项二是对xml中可能包含无效元素的任何内容使用base64编码。

#4


0  

Is your output file app.config supposed to be an XML file?

您的输出文件app.config是否应该是一个XML文件?

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'/>.

如果不是,那么应该使用文本输出方法而不是xml输出方法:使用

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

PS:这个问题似乎是如何在一个asp.net/c# app配置文件值中添加一个&号作为一个值的重复问题

#1


4  

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

XDocument正在做它应该做的事。

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

&是无效的XML。(这是一个未完成的字符/实体引用)

#2


3  

& means "Start of an entity" in XML so if you want to include an & as data you must express it as an entity — &amp; (or use it in a CDATA block).

&在XML中表示“开始一个实体”,因此如果您想包含&作为数据,您必须将它表示为一个实体& & &;(或者在CDATA块中使用)。

What you describe is normal behaviour and the XML would break otherwise.

您所描述的是正常的行为,否则XML将会崩溃。

#3


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.

有两个选项。要么确保正确的XML编码/解码XML文档中的所有内容。请记住,HTML和XML编码/解码略有不同。

Option two is to use base64 encoding on whatever content in the xml that might contain invalid elements.

选项二是对xml中可能包含无效元素的任何内容使用base64编码。

#4


0  

Is your output file app.config supposed to be an XML file?

您的输出文件app.config是否应该是一个XML文件?

If it is, then the & must be escaped as &amp;.

如果是,则&必须以& * *形式转义。

If it isn't, then you should be using the text output method instead of the xml output method: use <xsl:output method='text'/>.

如果不是,那么应该使用文本输出方法而不是xml输出方法:使用

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

PS:这个问题似乎是如何在一个asp.net/c# app配置文件值中添加一个&号作为一个值的重复问题