简体   繁体   中英

Reading xml Structure in sql server 2012

Sample Xml Structure

<?xml version="1.0" encoding="UTF-8"?>
<XmlSerializableHashtable xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <Entries>
      <Entry>
         <key xsi:type="xsd:string">col1</key>
         <value xsi:type="xsd:string">500</value>
      </Entry>
      <Entry>
         <key xsi:type="xsd:string">col2</key>
         <value xsi:type="xsd:string">0/60,1/1000</value>
      </Entry>
      <Entry>
         <key xsi:type="xsd:string">col3</key>
         <value xsi:type="xsd:string">localhost</value>
      </Entry>
   </Entries>
</XmlSerializableHashtable>

No i am trying to get value where key is col3

I am trying with the Xquery

Declare @x xml;
Set @x = (Select valuefrom sometable)

Select @x.query('/Entries/Entry/')

This Query will be helpful.

DECLARE @xmlData AS XML
DECLARE @sRawData AS VARCHAR(2000)

SET @sRawData = '<?xml version="1.0" encoding="UTF-8"?>
<XmlSerializableHashtable xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <Entries>
      <Entry>
         <key xsi:type="xsd:string">col1</key>
         <value xsi:type="xsd:string">500</value>
      </Entry>
      <Entry>
         <key xsi:type="xsd:string">col2</key>
         <value xsi:type="xsd:string">0/60,1/1000</value>
      </Entry>
      <Entry>
         <key xsi:type="xsd:string">col3</key>
         <value xsi:type="xsd:string">localhost</value>
      </Entry>
   </Entries>
</XmlSerializableHashtable>'

SET @xmlData = CAST(@sRawData AS XML)

SELECT MainDataCenter.Col.value('(key)[1]','varchar(max)') AS [key]
      ,MainDataCenter.Col.value('(value)[1]','varchar(max)') AS Value
FROM @xmlData.nodes('/XmlSerializableHashtable/Entries/Entry') AS MainDataCenter(Col)
WHERE MainDataCenter.Col.value('(key)[1]','varchar(max)') = 'col3'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM