I’m working in C# 3.5.
I have a XML document that contains a character entity within an element’s value. The reference is (LF). I do not want this expanded out into an actual linefeed, I want it to remain as an entity. This is because the commercial tool that processes the XML requires the LF to be represented as an entity, not an actual LF.
However, if I pass it anywhere near an XmlReader, it gets converted. So I switched to a StreamReader, but then it gets converted by XmlDocument.Load & XmlDocument.LoadXml.
I’ve tried setting the XmlResolver to null, prohibiting DTDs, and all sorts, but I’m unable to get the document to leave the entities alone.
Any ideas? I’m going to bodge this by munging the content on both ends with some double escaping, but I’d like to know if there’s a correct way of doing this.
(The reason it has to be Xml-ised at all is because it’s being sent via a webservice as an XmlNode….and I can’t change the API.)
You should use an
XmlWriterwith anXmlWriterSettingsobject whoseNewLineHandlingproperty is set toNewLineHandling.Entitize, and write the XML to a string. That’ll get you a string containing entities instead of LF characters.