you found your solution already. Just to get some light into this...
All statements like "change the reader, not the XML" are right of course. But sometimes we have to fullfill third party requirements we cannot change.
There are some tricks to deal with the generation of empty XML elements. Paste this into an empty query window and execute. Examine the results...
--The NULL-element is not there at all
SELECT 'text' AS filled
,'' AS empty
,NULL AS NotThere
FOR XML PATH('row');
--The NULL-element is rendered using "nil"
SELECT 'text' AS filled
,'' AS empty
,NULL AS NotThere
FOR XML PATH('row'),ELEMENTS XSINIL
--Look at this: Both columns are called "TheName". They are implicitly concatenated
SELECT 'a' AS TheName
,'b' AS TheName
FOR XML PATH('row')
--That leads to: Concatenate nothing with an empty string will at least return the empty string.
--this is other/better than ISNULL, because it will work with any type...
SELECT NULL AS TheName
,'' AS TheName
FOR XML PATH('row')
DECLARE @tbl TABLE(int1 INT, int2 INT);
INSERT INTO @tbl VALUES(NULL,1);
--int1 is missing
SELECT *
FROM @tbl
FOR XML PATH('row');
--both elements are there
SELECT int1, '' AS int1 --ISNULL(int1,'') would not compile...
,int2, '' AS int2
FROM @tbl
FOR XML PATH('row');
And finally: If you really need such a conversion you could create the XML, cast it to VARCHAR and use Replace
to change all <PropName />
to <PropName></PropName>
. I have to wash my hands now ;-)