用 T-SQL 建立 XML 時, 加入 CDATA 的 Sample

  • 2844
  • 0

XML文字內容只要用包在內的,XML 解析器就不會去解析裡面的內容,這樣的文字稱為Character Data Section,簡稱CDATA Section...

XML文字內容只要用<![CDATA[string]]>包在內的,XML 解析器就不會去解析裡面的內容,這樣的文字稱為Character Data Section,簡稱CDATA Section。

Sample Code

SELECT TOP 5
       1     AS Tag,
       null  AS Parent,
       id AS [DATA!1!ID!element],
       address AS [DATA!1!ADDRESS!CDATA]
FROM   comp
FOR    XML EXPLICIT, ROOT('ROOT')

Result

<ROOT>
  <DATA>
    <ID>1</ID>
    <ADDRESS><![CDATA[1 Claymore Drive, #08-05/06 Orchard Towers (Rear Block), Singapore 229594]]></ADDRESS>
  </DATA>
  <DATA>
    <ID>2</ID>
    <ADDRESS><![CDATA[No.1 Creation Rd. III, Secience-based Industrial Park, Hsinchu 300,Taiwan]]></ADDRESS>
  </DATA>
  <DATA>
    <ID>3</ID>
    <ADDRESS><![CDATA[Edificio Conata II, Calle Fructuos Gelabert 6-8, San Joan Despi,08970 Barcelona, Spain]]></ADDRESS>
  </DATA>
  <DATA>
    <ID>4</ID>
    <ADDRESS><![CDATA[SMC Networks Irvine, 20 Mason , Irvine, CA 92618, USA]]></ADDRESS>
  </DATA>
  <DATA>
    <ID>5</ID>
    <ADDRESS><![CDATA[5F#63 NO.421 HONGCAO RD.,SHANGHAI,CHINA]]></ADDRESS>
  </DATA>
</ROOT>