通过System.Xml.XmlWriter逃脱尖括号

时间:2020-03-05 18:54:24  来源:igfitidea点击:

我正在通过System.Xml.XmlWriter写一个包含一些XML的字符串。我被困使用WriteString(),并从文档中:

WriteString does the following:
  The characters &, <, and > are replaced with &amp;, &lt;, and &gt;, respectively.

我想停止此操作,但是似乎找不到任何XmlWriterSettings属性来控制此行为。有哪些解决方法?谢谢!

大卫

解决方案

回答

I'm stuck using WriteString(),

我认为这是我们问题的根源。我们能否进一步说明使用WriteString的原因?我的直觉是我们要使用的方法错误

回答

尝试将实际内容包装在CDATA标记之间:

<![CDATA [这是我的内容]]>

回答

WriteString将内容写为文字,并且&,<和>在XML文本中是非法的,因此将其转义。

如果另一端没有逃脱它们,那就是问题所在。

如果要编写未转义的XML,请使用WriteRaw。