How to prevent XmlSerializer from escaping < and > characters

我只是一个虾纸丫 提交于 2019-12-11 09:48:44

问题


I use an XlmSerializer to serialize a dotnet object.

One property of the dotnet object is a string with this value:

"<![CDATA[<p>No Comments</p>]]>"

Once serialized to a StringWriter, all the < and > characters are converted to &lt; and &gt; including the CDATA's.

How could I stop that from happening ?


回答1:


Don't put the CDATA in - that's the serializer's job. You've just told the serializer to make a valid XML out of the CDATA string. It does exactly that - after deserialization, you're still left with <![CDATA[<p>No Comments</p>]]>. That's exactly what you asked for! And more importantly, it's exactly what you want the serializer to do with the data - otherwise you'd be opening yourself to a world of hurt, because you'd need to ensure that the data is actually secure. In essence, you're performing double encoding.

Instead, just put <p>No Comments</p> there - and the serializer will handle the escaping for you, to make sure it's valid XML that actually deserializes to <p>No Comments</p>.



来源:https://stackoverflow.com/questions/25030591/how-to-prevent-xmlserializer-from-escaping-and-characters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!