你如何强迫C#xmlserializer转义撇号和引号? [英] How do you force C# xmlserializer to escape apostrophes and quotes?

查看:500
本文介绍了你如何强迫C#xmlserializer转义撇号和引号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到一种情况,客户端需要将和字符分别以XML格式转义为和。 XMLSerializer是(显然)写的,因为在大多数情况下并不严格要求它们,因此它们会忽略它们。当然,我不是这个星球上唯一有这个问题的人......你如何解决它? 请不要回答没必要。我已经知道了。这是客户要求。



感谢您的评论。我没有注意到这个问题实际上已经删除了我的部分问题。我希望成为$ quot;[没有空格]和'成为'$';[没有空格]。



我有什么尝试过:



- 手动转义字符:愚蠢的系统解构它们并逃脱我的&符号



- 结果的后序列化解析:我不想这样做,我希望系统能够完成工作



- 相当一点谷歌搜索:我发现大多是荒谬的答案,说你不需要那样做。是的,我这样做。我的客户需要它。

I have a situation where a client requires the " and ' characters to be escaped in XML explicitly into " and ' respectively. XMLSerializer was written (apparently) with the idea in mind that since these are not strictly required in most circumstances, they would ignore them. Surely I'm not the only person on the planet with this problem... How do you solve it? And please don't answer with "it's not necessary." I know that already. It's a client requirement.

Thanks for the comment. I hadn't noticed that the question had actually removed part of my question. I want " to become "$ quot ;" [no spaces] and ' to become "$ apos ;" [no spaces].

What I have tried:

- manually escaping characters: the stupid system deconstructs them and escapes my ampersand

- post-serialization parsing of the result: I'd prefer not to do it this way, I'd like the system to do the work

- quite a bit of Googling: I've found mostly nonsensical "answers" that say "you don't need to do that." Yes I do. My client requires it.

推荐答案

; [无空格]和'成为'
quot ;" [no spaces] and ' to become "


'; [没有空格]。



我尝试了什么:



- 手动转义字符:愚蠢的系统解构它们并逃脱我的&符号



- 序列化后解析结果:我不想这样做,我'喜欢系统做的工作



- 相当多的谷歌搜索:我发现大多是荒谬的答案,说你不需要去做。是的,我愿意。我的客户端需要它。
apos ;" [no spaces].

What I have tried:

- manually escaping characters: the stupid system deconstructs them and escapes my ampersand

- post-serialization parsing of the result: I'd prefer not to do it this way, I'd like the system to do the work

- quite a bit of Googling: I've found mostly nonsensical "answers" that say "you don't need to do that." Yes I do. My client requires it.


我不明白'escape'是什么意思(比如C#string \ n \\\?),但是XmlSerializer会将特殊字符转换为它的实体值自动,否则它不是有效的XML ...



更新



你必须理解(并解释客户)引号和双引号是XML中的合法字符,这就是序列化程序不接触它们的原因......

我能看到的唯一选择是编写自己的serizlizer : IXmlSerializable Interface(System.Xml.Serialization) [ ^ ]
I do not understand what do you mean by 'escape' (like in C# string \n\r?), but XmlSerializer converts special characters to it's entity value automatically, otherwise it would not be a valid XML...

UPDATE

You have to understand (and explain the customer) that quotes and double-quotes are legal characters in XML, that's the reason the serializer do not touch them...
The only options I can see is writing your own serizlizer: IXmlSerializable Interface (System.Xml.Serialization)[^]


这篇关于你如何强迫C#xmlserializer转义撇号和引号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆