用 query 方法 获得xml 节点的值

DECLARE @result xml
SET @result='<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"><s:Body><ReceiveDataResponse xmlns="http://tempuri.org/"><ReceiveDataResult xmlns:a="http://schemas.datacontract.org/2004/07/Cenvation.ServiceWebsite.Datamodel" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><a:ID>17953736-852b-e511-80cc-0050568e38e0</a:ID><a:Message i:nil="true"/><a:SchemaName>contact</a:SchemaName><a:Success>true</a:Success></ReceiveDataResult></ReceiveDataResponse></s:Body></s:Envelope>'
SELECT @result.query('declare namespace s="http://schemas.xmlsoap.org/soap/envelope/";
                      declare namespace a="http://schemas.datacontract.org/2004/07/Cenvation.ServiceWebsite.Datamodel";
                      declare namespace i="http://www.w3.org/2001/XMLSchema-instance";
                      declare default element namespace "http://tempuri.org/"; data(/s:Envelope/s:Body/ReceiveDataResponse/ReceiveDataResult/a:ID)')

这是一个读取xml 中指定节点数据的一段脚本,这个演示样例数据中包括命名空间,所以在query 方法中须要声明这些命名空间。否则无法正确解析这段xml, 假设xml 数据中包括命名空间,就能够不用在query 方法中声明这些命名空间。

原文地址:https://www.cnblogs.com/cxchanpin/p/7086354.html