您已經找到了您的解決方案。只是爲了弄清楚這一點...
所有的聲明,如「改變讀者,而不是XML」,當然是正確的。但有時我們必須滿足我們無法改變的第三方要求。
有一些技巧來處理生成空的XML元素。將其粘貼到空的查詢窗口中並執行。檢查結果...
--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');
最後:如果你真的需要這樣的轉換,你可以創建XML,它轉換爲VARCHAR,並使用Replace
改變所有<PropName />
到<PropName></PropName>
。我現在必須洗手;-)
使用sigle incline quote來指示代碼'\'' –
' '和' '是100%的同一件事情。除非**用於處理XML的工具不合適或不合適,否則不必使用其中一種**。在這種情況下,你必須修復你的工具,而不是你的XML。 –
Tomalak
@Tomalak OP已經解決了這個問題'我知道這不是標準,但我有一個商業原因' –