The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My badges

  • Twitter Updates

  • My Flickr Stream

  • Pages

  • All categories

  • Email Subscription

    Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 717 other subscribers

Don’t roll your own XML with string functions (via delphi – Writing XML String with proper formatting? – Stack Overflow)

Posted by jpluimers on 2026/11/18

I still stand with my almost 15 year old XML answer [Wayback/Archive] delphi – Writing XML String with proper formatting? – Stack Overflow

Do not generate XML by hand PERIOD.

Writing correct code for escaping complex data (for instance XML, HTML or other SGML in XML, escaped CDATA) is not worth it.

The escaping you do is just a start. Wait until someone puts something in your data that is incompatible.

Many databases support creating well formed XML from queries anyway (see the other answers), that is a direction you should be looking into.

Transforming input data into well formed XML document (let alone valid XML) is way too complex because XML is a relatively complex language including many features making assembling a well formed XML document harder:

  • escaping
  • valid characters (almost all Unicode characters are valid)
  • CDATA
  • embedded XML

The only circumstance where it could temporarily work is under the predondiction that you are absolutely sure the input data will never interfere with handling the above. The problem is: input data rarely is static, so future changes will usually void that precondition.

The reason that XML libraries, which usually are highly optimised, keep getting updates is that the exceptions to those preconditions are so widespread. You will never be able to keep up with a not-invented-here syndrome self-written solution.

It means I wholeheartedly disagree with these comments:

  • I would personally don’t include an XML library for generating one and only one single XML file. In that case I would rather use the way shown by Jerry and I definitely agree with Deltics’ comment.
  • @Ken, you can’t get much more lightweight than hand-crafted XML. If there is lots of this sort of thing in the OP’s application then adopting an XML library might make sense, but if this is an isolated requirement then for what they are doing, roping in a complete XML library would be like hiring a 40 ton truck to move 40 kg of animal feed. ymmv. :) – Deltics

Hence my own comment to my own answer:

That depends: if you are absolutely certain of the source of your data you might want to do it. In my experience your users will catch up with an incomplete solution faster than you expect.

So I do fully agree with this comments:

  • The terminology you’re looking for is called “escaping” or “encoding”, and writing your own XML writing or parsing code is a serious waste of time. There are dozens of lightweight XML libraries out there that are free (OmniXML, for instance). This sounds like a severe case of NIH (not invented here) syndrome. :-)
  • Here’s the list : en.wikipedia.org/wiki/List_of_XML_and_HTML_character_entity_references#Predefined_entities_in_XML – but you should really make things easier on yourself and find an XML library.
  • Personally I’d use an XML library because that makes the code more readable and maintainable. I notice that you used a string list and lots of StringReplace calls. If you so concerned about memory why didn’t you optimise those out? And why are you using TADOQuery? Loads of memory used there. A good lightweight XML emitter would use less memory than this code! This is pointless micro (non)-optimisation.

Example XML library links (via [Wayback/Archive] maxtoroq/dotnet-xml: A list of XML libraries, frameworks and tools for .NET):

Especially the last bit of the repository is important as it shows what kind of testcases you need to write stable code.

Related blog post: XML and HTML escapes

Related StackOverflow answer:

Thanks:

–jeroen

Leave a Comment