2

需要一点帮助才能在 SQL Server 中创建一些 XML。

生成的 XML 应如下所示:

<?xml version="1.0" encoding="utf-8"?>
<Root Version="2.17" KFZ="XX-DD 389">
  <Head name="ExecuteAnswer">
    <Key name="AnswerStatus" value="-OK" />
    <Key name="MsgId"><![CDATA[KFHB0907896aAUH223]]></Key>
    <Section name="Command">
      <Key name="Name" value="SetNewCommand" />
      <Key name="Param0"><![CDATA[XX-DD 389]]></Key>
      <Key name="Param1"><![CDATA[]]></Key>
      <Key name="Param2"><![CDATA[0987asfdsafhdsSS]]></Key>
      <Key name="Param3"><![CDATA[0097aSSSHSDOPir0]]></Key>
    </Section>
  </Head>
  <Data></Data>
</Root>

我被困在添加<Section>到 XML 中,如果我添加它,[Section!3!name]我得到这个错误:

错误 -> 键将被部分覆盖

代码:

SELECT 
   1 AS Tag, 
   NULL AS Parent,
   '2.17' AS [Root!1!Version],
   'XX-DD 389' AS [Root!1!KFZ],
   NULL AS [Head!2!name!Element],
   NULL AS [Key!3!name],
   NULL AS [Key!3!value],
   NULL AS [Key!3!CDATA]

UNION ALL               

SELECT 
    2 as Tag,  
    1 as Parent,    
    NULL,
    NULL,
    'ExecuteAnswer',
    NULL,
    NULL,
    NULL

UNION ALL 

SELECT 
    3 as Tag,  
    2 as Parent,    
    NULL,           
    NULL,           
    NULL,           
    'AnswerStatus',
    '-OK',
    NULL

UNION ALL 

SELECT 
    3 as Tag,  
    2 as Parent,    
    NULL,           
    NULL,           
    NULL,           
    'MsgId',
    NULL,           
    'KFHB0907896aAUH223'
FOR XML EXPLICIT

有人可以帮我吗?

问候亚历克斯

4

1 回答 1

1

正如评论中所指出的那样,没有理由CDATA再......它在语义上与正确转义的普通 text()节点完全相同。

但是,有时遗留系统或第三方工具需要它。

您必须知道的事情:除了强制转换并手动附加它之外,没有机会将其添加xml declaration到 XML 中。NVARCHAR(MAX)

因此,如果您无论如何都必须检查字符串,您可以创建干净且简单的XML FOR XML PATH(),创建CDATAas<xdata>content</xdata>并简单REPLACE地将这些占位符更改为CDATA开始和结束字符。

但是:这是一种方法EXPLICIT

SELECT 
   1 AS Tag, 
   NULL AS Parent,
   '2.17' AS [Root!1!Version],
   'XX-DD 389' AS [Root!1!KFZ],
   NULL AS [Head!2!name!Element],
   NULL AS [Key!3!name],
   NULL AS [Key!3!value],
   NULL AS [Key!3!!CDATA],
   NULL AS [Section!4!name],
   NULL AS [Key!5!name],
   NULL AS [Key!5!value],
   NULL AS [Key!6!name],
   NULL AS [Key!6!!CDATA]

UNION ALL               

SELECT 
    2 as Tag,  
    1 as Parent,    
    NULL,
    NULL,
    'ExecuteAnswer',
    NULL,
    NULL,
    NULL,
    NULL,
    NULL,
    NULL,
    NULL,
    NULL

UNION ALL 

SELECT 
    3 as Tag,  
    2 as Parent,    
    NULL,           
    NULL,           
    NULL,           
    'AnswerStatus',
    '-OK',
    NULL,
    NULL,
    NULL,
    NULL,
    NULL,
    NULL

UNION ALL 

SELECT 
    3 as Tag,  
    2 as Parent,    
    NULL,           
    NULL,           
    NULL,           
    'MsgId',
    NULL,           
    'KFHB0907896aAUH223',
    NULL,
    NULL,
    NULL,
    NULL,
    NULL

    UNION ALL 

SELECT 
    4 as Tag,  
    2 as Parent,    
    NULL,           
    NULL,           
    NULL,           
    NULL,
    NULL,           
    NULL,
    'Command',
    NULL,
    NULL,
    NULL,
    NULL

    UNION ALL 

SELECT 
    5 as Tag,  
    4 as Parent,    
    NULL,           
    NULL,           
    NULL,           
    NULL,
    NULL,           
    NULL,
    NULL,
    'Name',
    'SetNewCommand',
    NULL,
    NULL

    UNION ALL 

SELECT 
    6 as Tag,  
    4 as Parent,    
    NULL,           
    NULL,           
    NULL,           
    NULL,
    NULL,           
    NULL,
    NULL,
    NULL,
    NULL,
    'Param0',
    'XX-DD 389'
FOR XML EXPLICIT
于 2018-02-02T11:57:53.903 回答