2014-01-10 46 views
2

我正在解析一個xml文本,其中包含像'áéñ'這樣的字符。Sql 2008,無效字符解析xml,帶字符的波浪號

我收到'在文本內容中發現無效字符'。錯誤,這樣

declare @Xml varchar(100) 

set @Xml = 
' 
<?xml version="1.0" encoding="UTF-8"?> 
<Root>á</Root> 
' 

declare @XmlId integer 

execute dbo.sp_xml_preparedocument @XmlId output, @Xml 

select * from openXml(@XmlId, '/', 2) with (
    Root varchar(10) 
) 
execute dbo.sp_xml_removedocument @XmlId 

而且我發現了以下錯誤:

The XML parse error 0xc00ce508 occurred on line number 3, near the XML text "<Root>". 
Msg 6602, Level 16, State 2, Procedure sp_xml_preparedocument, Line 1 
The error description is 'An invalid character was found in text content.'. 
Msg 8179, Level 16, State 5, Line 13 
Could not find prepared statement with handle 0. 
Msg 6607, Level 16, State 3, Procedure sp_xml_removedocument, Line 1 
sp_xml_removedocument: The value supplied for parameter number 1 is invalid. 

是否有某種方式,SQL可以解析這個XML?或者問題是編碼?

是對這些字符進行編碼的唯一解決方案還是有更好的方法來解決它?

回答

-1

字符á字符不是UTF-8編碼的有效有效字符。這是任何XML驗證器應該告訴你的。解決方案是正確編碼。

如果您從其他人處獲取數據,您應該告訴他們他們做錯了。如果你生成這些數據,你應該解決這個問題。假設你被困在中間,有可能爲該文件編寫一個預處理器,用於「修復」無效的XML,然後將其交給需要有效XML的進程(供應商不願意或無法提供有效的XML時應該避免可能的話)

ADDED

你會在一個追求成功說服TSQL解析XML將無法驗證。

2

今天我得到了同樣的錯誤,將序列化對象作爲xml傳遞給我的存儲過程。最終我發現錯誤在哪裏。

declare @Xml varchar(100) 

要:

從改變你的代碼

declare @Xml nvarchar(100) 

下面是總結我在網上找到,希望能幫助你。

一個nvarchar列可以存儲任何Unicode數據。 A varchar列僅限於8位代碼頁(非Unicode字符數據)。使用nvarchar而不是varchar可以幫助您避免在每次讀取或寫入數據庫時​​都進行編碼轉換。

+0

打印出你的@Xml,你應該看到這個 – Paparazzi