Click here to Skip to main content
15,885,767 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.
Posted
Updated 25-Jun-18 4:19am
v2
Comments
F-ES Sitecore 25-Jun-18 9:53am    
If your client required you to walk on water would you endlessly google for how to do it, or would you simply tell them their requirement is unreasonable? It's the client you need to manage, not the code.
James McCullough 25-Jun-18 10:00am    
Thank you. It's precisely this kind of answer I was hoping to avoid. As I said in the question, surely I'm not the only person on the planet with this problem...
Dave Kreskowiak 25-Jun-18 10:17am    
It may not be required by the XML standard, but instead may be required by a third party system using the XML file saved by his app.
F-ES Sitecore 25-Jun-18 10:23am    
Then he should stipulate that. "My client requires it" makes it seem like there is no reason beyond that, and that's not a reason. If you want me to spend my time on fixing a problem for you there had better be a valid reason.

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)[^]
 
Share this answer
 
v2
Comments
James McCullough 25-Jun-18 9:41am    
Updated the question. Sorry, didn't realize the question system had removed part of my original question.
Kornfeld Eliyahu Peter 25-Jun-18 10:05am    
I see. Updated...
James McCullough 25-Jun-18 10:46am    
Thank you. I found some references to doing this but figured I would ask here in case there was just a setting I was missing or something like that. Thanks for the answer!
Like Peter up in Solution 1 said, you're going to have to write your own custom serializer to handle the customer required changes. Just Google for "C# custom xml serializer" and you'll find plenty of examples.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900