• I think you have added an abstraction layer that seems to work but at the end of the day you still have an entitization problem:

    I think your approach has the problem of not differentiating when a character should be entitized, i.e. when a left-angle bracket is part of some markup or a comment versus when < is part of a string literal, however the XML parser within SQL Server that is invoked when you use TYPE as part of FOR XML does know that and it can do that entitization for us properly.

    There are no special teachers of virtue, because virtue is taught by the whole community.
    --Plato