2015-08-09 58 views
5

如何修正錯誤非法限定名稱的字符,該樣品中:鑄文字XML錯誤非法限定名稱字符

Declare @Str As nvarchar(256) 
Set @Str = N'<Log "ReceiptStockHNo="2" ReceiptStockHDate="Feb 4 2" Comment="" />' 
Select Cast(@Str As xml) 

錯誤:

消息9455,級別16,狀態1,5號線
XML分析:行1,字符6,非法限定名稱字符

回答

5

這是什麼額外"? 。

enter image description here

取出",它會工作。

其他信息:

爲了防止未來的錯誤對需要編碼的字符,如&<,使用適當的替換:

Declare @Str As nvarchar(256) 
Set @Str = '<tag>&</tag>' 
Select Cast(@Str As xml) 

將產生:

消息9421,級別16 ,狀態1,行3 XML解析:行1,字符7
非法名字字符

當改變<&lt;

Declare @Str As nvarchar(256) 
Set @Str = '<tag>&lt;</tag>' 
Select Cast(@Str As xml) 

都會好的。

+0

,感謝的答案,如何刪除「字符與T-SQL – Iraj

+0

@Iraj我相信這是一個新的問題,因此問它作爲一個?新的問題。(但在問之前選擇):-) –

1

注意:這不是一個答案,而是一個擴展的評論。

婁串

N'<Log "ReceiptStockHNo="2" ReceiptStockHDate="Feb 4 2" Comment="" />' 

它不是一個有效的XML,因爲""ReceiptStockHNo(第一屬性的名稱)。 如果XML數據是內置的使用字符串連接並且不使用專用的XML API /功能,則可能會發生這種情況。

例如,如果(1)屬性名稱被存儲在作爲TABLE.COLUMN

"ReceiptStockHNo 

和(2)來構建XML文檔/使用片段字符串連接我們可能得到一個無效的XML。一種解決方案可能是FOR XML。見下面的例子和尾註:

DECLARE @Table1 TABLE(
    ID INT NOT NULL PRIMARY KEY, 
    RowType TINYINT NOT NULL, -- 1 = Log [record] 
    Attribute1 NVARCHAR(100) NOT NULL, 
    Attribute1_Value INT, 
    Attribute2 NVARCHAR(100) NOT NULL, 
    Attribute2_Value DATETIME 
) 
INSERT @Table1 
VALUES (123, 1, N'"ReceiptStockHNo', 2, N'ReceiptStockHDate', '2014-02-04 00:00:00.000') 

-- Get data as XML: method #1 (wrong) 
DECLARE @x NVARCHAR(256) 
SELECT @x = N'<Log ' + t.Attribute1 + '="' + CONVERT(VARCHAR(11), t.Attribute1_Value) + '" ' + t.Attribute2 + '="' + CONVERT(VARCHAR(25), t.Attribute2_Value) + '" Comment="" />' 
FROM @Table1 t 
WHERE t.ID = 123 
AND  t.RowType = 1 -- Log [record] 

SELECT @x AS [Get data as XML: method #1 (wrong)] 
SELECT 'Convert to XML' AS [Message] 
BEGIN TRY 
    SELECT CONVERT(XML, @x) AS [Convert to XML result] 
END TRY 
BEGIN CATCH 
    SELECT ERROR_MESSAGE() AS [Get data as XML: method #1 (wrong) - ERROR_MESSAGE] 
END CATCH 

-- Get data as XML: method #2 (ok) 
SET @x = N'' 
SET @x = 
(
    SELECT t.Attribute1_Value AS '"ReceiptStockHNo', 
      t.Attribute2_Value AS 'ReceiptStockHDate', 
      '' AS Comment 
    FROM @Table1 t 
    WHERE t.ID = 123 
    AND  t.RowType = 1 -- Log [record] 
    FOR XML RAW('Log') 
) 

SELECT @x AS [Get data as XML: method #2 (ok)] 
SELECT 'Convert to XML' AS [Message] 
SELECT CONVERT(XML, @x) AS [Convert to XML result] 

輸出:

Get data as XML: method #1 (wrong) 
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 
<Log "ReceiptStockHNo="2" ReceiptStockHDate="Feb 4 2014 12:00AM" Comment="" /> 

Message 
-------------- 
Convert to XML 

Convert to XML result 
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 

Get data as XML: method #1 (wrong) - ERROR_MESSAGE 
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 
XML parsing: line 1, character 6, illegal qualified name character 

Get data as XML: method #2 (ok) 
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 
<Log _x0022_ReceiptStockHNo="2" ReceiptStockHDate="2014-02-04T00:00:00" Comment=""/> 

Message 
-------------- 
Convert to XML 

Convert to XML result 
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 
<Log _x0022_ReceiptStockHNo="2" ReceiptStockHDate="2014-02-04T00:00:00" Comment="" /> 

當我使用FOR XML生成的XML數據我得到不同的結果,因爲FOR XML編碼保留XML字符(包括那些從qualified names)。在這種情況下,"被編碼爲_x0022_

"ReceiptStockHNo_x0022_ReceiptStockHNo