2012-01-04 80 views
1

如何将已经是xml的字符串存储为另一个xml的根节点的属性?SQL Server 2008呈现嵌套的XML

我想存储的字符串是@inputXmlString。它的值是:

<?xml version="1.0" encoding="utf-8"?> 
<OneViewReviewRq 
    xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:xsd = "http://www.w3.org/2001/XMLSchema"> 
<LOAN>ASFDK</LOAN> 
</OneViewReviewRq> 

我有这个值作为存储到另一个节点的属性说row,这是根本点。这是所需的输出应该是什么:

<root oneViewXml="<?xml version="1.0" encoding="utf-8"?> 
<OneViewReviewRq 
    xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:xsd = "http://www.w3.org/2001/XMLSchema"> 
<LOAN>ASFDK</LOAN> 
</OneViewReviewRq>" /> 

的问题是:当我使用for xml pathfor xml raw的报价和<>字符不会呈现为是但&lt;&gt;

这是我使用的查询:

select @OutputXml=(select @inputXmlString as '@oneViewXml' 
for xml path('root')) 

这是输出:

<root oneViewXml="&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;&#xD;&#xA;&lt;OneViewReviewRq&#xD;&#xA; xmlns:xsi = &quot;http://www.w3.org/2001/XMLSchema-instance&quot;&#xD;&#xA; xmlns:xsd = &quot;http://www.w3.org/2001/XMLSchema&quot;&gt;&#xD;&#xA;&lt;LOAN&gt;ASFDK&lt;/LOAN&gt;&#xD;&#xA;&lt;/OneViewReviewRq&gt;" /> 

回答

0

这完全是因为它应该是因为<>&是XML属性中的无效字符。当您查询您的XML的属性值时,您将返回XML原样。

试试这个:

declare @inputXmlString nvarchar(max) = 
'<?xml version="1.0" encoding="utf-8"?> 
<OneViewReviewRq 
    xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:xsd = "http://www.w3.org/2001/XMLSchema"> 
<LOAN>ASFDK</LOAN> 
</OneViewReviewRq>' 

declare @OutputXml xml 

select @OutputXml=(select @inputXmlString as '@oneViewXml' 
for xml path('root')) 

select @OutputXml.value('root[1]/@oneViewXml', 'nvarchar(max)') 

http://data.stackexchange.com/stackoverflow/qt/123198/

+0

感谢Mikael..what你说的作品完美。 – anshul2181981 2012-01-05 15:17:43