3

我在名为“AlgorithmLog”的表的 [XMLValue] 列中有类似以下代码:

<?xml version="1.0" encoding="utf-8"?>
<AdapterInfo xmlns:i="http://www.w3.org/2001/XMLSchema-instance"     
            xmlns:d1p1="http://schemas.datacontract.org/2004/07/Adapters.Adapter.CloudTrader"    
            xmlns="http://schemas.datacontract.org/2004/07/Adapters.Adapter"  
            i:type="d1p1:AlgorithmStatusReport">
    <SequenceNumber>0</SequenceNumber>
    <TrackingGuid i:nil="true" />
    <d1p1:Broker>Default</d1p1:Broker>
    ...
       <d1p1:XMLValue>&lt;?xml version="1.0"?&gt;&lt;int xmlns="http://schemas.microsoft.com/2003/10/Serialization/"&gt;1900&lt;/int&gt;</d1p1:XMLValue>
</AdapterInfo>

我想在节点内获取值“1900”<d1p1:XMLValue>

所以这是我的查询:

WITH XMLNAMESPACES('http://schemas.datacontract.org/2004/07/Adapters.Adapter' AS x, 
                   'http://schemas.datacontract.org/2004/07/Adapters.Adapter.CloudTrader' As p,
                   'http://schemas.microsoft.com/2003/10/Serialization/'as w)
   SELECT  
       XMLValue.query('(/x:AdapterInfo/p:XMLValue/w:int)[1]')AS [XMLVaule]
   FROM AlgorithmLog 

但它什么也不返回。

谁能告诉我我在哪里做错了或者我该怎么做?

谢谢你。

4

1 回答 1

2

由于您已经在另一个 XML 节点中“编码”了 XML,并且您无法XML使用.value()XQuery 方法自动转换为数据类型,所以这一切都涉及到了一点——但这似乎对我有用:

;WITH XMLNAMESPACES('http://schemas.datacontract.org/2004/07/Adapters.Adapter' AS x, 
                    'http://schemas.datacontract.org/2004/07/Adapters.Adapter.CloudTrader' As p,
                    'http://schemas.microsoft.com/2003/10/Serialization/'as w)
   SELECT  
       CAST(XmlContent.value('(/x:AdapterInfo/p:XMLValue)[1]', 'varchar(2000)') AS XML).value('(w:int)[1]', 'int') AS [XMLValue]
   FROM AlgorithmLog
   WHERE .......  -- use whatever condition makes sense for you here
于 2012-07-03T21:03:59.220 回答