将文本转换为xml错误非法限定名称字符

时间:2015-08-09 06:29:55

标签: sql sql-server tsql

如何修复此示例中的错误非法限定名称字符:

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

错误:

  

Msg 9455,Level 16,State 1,Line 5
  XML解析:第1行,第6个字符,非法限定名称字符

2 个答案:

答案 0 :(得分: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) 

会好的。

答案 1 :(得分:1)

注意:这不是答案,而是扩展评论。

Bellow string

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

由于来自"的{​​{1}}(第一个属性的名称),因此它不是有效的XML。 如果XML数据使用字符串连接构建而不使用专用的XML API /功能,则可能会发生这种情况。

例如,如果(1)属性名称存储在table.column中作为

"ReceiptStockHNo

和(2)构建XML文档/片段使用字符串连接我们可能会得到无效的XML。一种解决方案可能是FOR XML。请参阅下一个示例和结束注释:

"ReceiptStockHNo

输出:

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="" /> 生成XML数据时,我会得到不同的结果,因为FOR XML编码保留的XML字符(包括来自FOR XML的字符)。在这种情况下,qualified names被编码为"

_x0022_"ReceiptStockHNo