我在将 xml 从 .net 传递到存储过程时遇到问题,下面是传递的 xml
DECLARE @XMLDoc AS XML
SET @XMLDoc =
'<ArrayOfProblemType xmlns:i="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://schemas.datacontract.org/2004/07/DatasetServices.JobServices.DataContracts">
<ProblemType>
<DateAdded>2011-08-10T11:19:35.187</DateAdded>
<EmailTemplate>testing email template</EmailTemplate>
<ProblemDescription>No Money</ProblemDescription>
<ProblemTypeId>4</ProblemTypeId>
<SendEmail>true</SendEmail>
</ProblemType>
</ArrayOfProblemType>'
我正在尝试按如下方式提取数据
SELECT
ProblemData.Probs.value('(ProblemTypeId)[1]', 'int') as 'ID',
ProblemData.Probs.value('(DateAdded)[1]', 'Varchar(50)') as 'DateAdded'
FROM
@XMLDoc.nodes('/ArrayOfProblemType/ProblemType') as ProblemData(Probs)
我没有收到任何错误,但我也没有收到任何数据。
我查看了提示给我的所有答案,但似乎没有一个在 xml 中包含 xmlns namespace 。我怀疑我需要以某种方式删除它。有没有人遇到过这个可以提供帮助?
谢谢 保罗
最佳答案
您没有遵守 XML namespace - 您需要这样做!
试试这个:
;WITH XMLNAMESPACES('http://schemas.datacontract.org/2004/07/DatasetServices.JobServices.DataContracts' AS ns)
SELECT
ProblemData.Probs.value('(ns:ProblemTypeId)[1]', 'int') as 'ID',
ProblemData.Probs.value('(ns:DateAdded)[1]', 'Varchar(50)') as 'DateAdded'
FROM
@XMLDoc.nodes('/ns:ArrayOfProblemType/ns:ProblemType') as ProblemData(Probs)
关于sql-server - 在从 .net 传递到 SQL Server 的存储过程中解析 Xml 时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7010319/