发生XML解析错误0xc00ce01d

时间:2011-06-13 12:53:02

标签: sql-server

我收到xml解析器错误。如何处理这种情况。

我的脚本是

DECLARE @xmlDoc VARCHAR(MAX)
DECLARE @handle INT
SET @xmlDoc = N'
<DELETED>
  <JID>41185</JID>
  <WID>0</WID>
  <AccountReference>LH169</AccountReference>
  <OEReference>Ari002</OEReference>
  <InvoiceNumber>0</InvoiceNumber>
  <OrderPlacedBy>Mark Catterall</OrderPlacedBy>
  <Specialist>0FFICINA MOTTAUTO</Specialist>
  <UserName xsi:nil="true" />
</DELETED>'

Declare @SqlQuery varchar(max)

EXEC sp_xml_preparedocument @handle OUTPUT, @xmlDoc
SET @SqlQuery ='Select '
SELECT  @SqlQuery = @SqlQuery + LocalName  + ',' FROM OPENXML (@handle, '/DELETED', 1) 
WHere  localname not like '#%' And id>0 And localname <> 'nil'
Set @SqlQuery = Substring(@SqlQuery,0,LEN(@SqlQuery)-1) + ' From tablename'
print @SqlQuery
--Exec @SqlQuery
--EXEC sp_xml_removedocument @handle

我在<UserName xsi:nil="true" />如何处理这种情况时收到错误。什么要在剧本中纠正。

感谢

1 个答案:

答案 0 :(得分:1)

  

0xC00CE01D:未声明的引用   名称空间前缀。

将XML更改为此将负责

<DELETED xmlns:xsi="uri">
  <JID>41185</JID>
  <WID>0</WID>
  <AccountReference>LH169</AccountReference>
  <OEReference>Ari002</OEReference>
  <InvoiceNumber>0</InvoiceNumber>
  <OrderPlacedBy>Mark Catterall</OrderPlacedBy>
  <Specialist>0FFICINA MOTTAUTO</Specialist>
  <UserName xsi:nil="true" />
</DELETED>'

使用XML数据类型可以改为:

declare @xml xml = 
'<DELETED xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <JID>41185</JID>
  <WID>0</WID>
  <AccountReference>LH169</AccountReference>
  <OEReference>Ari002</OEReference>
  <InvoiceNumber>0</InvoiceNumber>
  <OrderPlacedBy>Mark Catterall</OrderPlacedBy>
  <Specialist>0FFICINA MOTTAUTO</Specialist>
  <UserName xsi:nil="true" />
</DELETED>'

declare @SqlQuery varchar(max)
set @SqlQuery ='Select '

select 
  @SqlQuery = @SqlQuery + N.value('local-name(.)', 'varchar(max)')+','
from @xml.nodes('DELETED/*') as T(N)

set @SqlQuery = substring(@SqlQuery,0,len(@SqlQuery)-1) + ' from tablename'

print @SqlQuery