2013-07-06 88 views
9

您好我是新來的XML替換「<」和「>」與「<」 and 「>」在SQL Server

我有這樣

SELECT ProjectId, 
     ProjectCode, 
     ProjectName, 
     TechId, 
     -- LocationId, 

     (SELECT GeoId,PoliticalDivisionId ,GeographicLocationName,IsoCode,Longitude,Latitude,ParentLocationId, 
     t2.CreatedBy,t2.CreatedOn,t2.LastUpdatedBy,t2.LastUpdatedOn 
    FROM GeographicLocation t2 
    WHERE GeoId = t1.LocationId 
    FOR XML PATH('Location')), 
     RtoId, 
     CreatedBy, 
     CreatedOn, 
     LastUpdatedBy, 
     LastUpdatedOn 
FROM Project t1 
where ProjectId=1 
FOR XML PATH('ProjectInfo') 

查詢它返回的XML作爲

<ProjectInfo> 
<ProjectId>1</ProjectId> 
<ProjectCode>US-W1-00001</ProjectCode> 
<ProjectName>Rees</ProjectName> 
<TechId>1</TechId> 
&lt;Location&gt;&lt;GeoId&gt;235&lt;/GeoId&gt;&lt;PoliticalDivisionId&gt;2&lt;/PoliticalDivisionId&gt;&lt;GeographicLocationName&gt;UNITED STATES&lt;/GeographicLocationName&gt;&lt;IsoCode&gt;US&lt;/IsoCode&gt;&lt;/Location&gt; 
<RtoId>3</RtoId> 
<CreatedBy>1</CreatedBy> 
<CreatedOn>2013-06-30T20:55:21.587</CreatedOn> 
<LastUpdatedBy>1</LastUpdatedBy> 
<LastUpdatedOn>2013-06-30T20:55:21.587</LastUpdatedOn> 

prject標籤以<和>的形式顯示。不過地點內標籤顯示爲「<」和「>」我怎麼跟<代替他們和>

更新:有是問題的一個小錯誤。內部XML不是爲rtoid,它是定位

我更新查詢作爲

SELECT ProjectId, 
     ProjectCode, 
     ProjectName, 
     TechId, 
     -- LocationId, 

     replace(replace((SELECT GeoId,PoliticalDivisionId ,GeographicLocationName,IsoCode,Longitude,Latitude,ParentLocationId, 
     t2.CreatedBy,t2.CreatedOn,t2.LastUpdatedBy,t2.LastUpdatedOn 
    FROM GeographicLocation t2 
    WHERE GeoId = t1.LocationId 
    FOR XML PATH('Location')), '&lt;', '<'), '&gt;', '>'), 
     RtoId, 
     CreatedBy, 
     CreatedOn, 
     LastUpdatedBy, 
     LastUpdatedOn 
FROM Project t1 
where ProjectId=1 
FOR XML PATH('ProjectInfo') 

但還是它的同

回答

8

,我已經找到了辦法是明確替換它們:

select ProjectId, ProjectCode, ProjectName, TechId, 
     replace(replace(RtoId, '&lt;', '<'), '&gt;', '>') as RtoId, 
     . . . 
from (<your query here>) 
3
SELECT ProjectId, 
     ProjectCode, 
     ProjectName, 
     TechId, 
     -- LocationId, 
     replace(replace((SELECT GeoId,PoliticalDivisionId ,GeographicLocationName,IsoCode,Longitude,Latitude,ParentLocationId, 
     t2.CreatedBy,t2.CreatedOn,t2.LastUpdatedBy,t2.LastUpdatedOn 
    FROM GeographicLocation t2 
    WHERE GeoId = t1.LocationId 
    FOR XML PATH('Location')), '&lt;', '<'), '&gt;', '>') 
     RtoId, 
     CreatedBy, 
     CreatedOn, 
     LastUpdatedBy, 
     LastUpdatedOn 
FROM Project t1 
where ProjectId=1 
FOR XML PATH('ProjectInfo') 
10

我認爲正確的方法是使用TYPE Directive

SELECT ProjectId, 
     ..., 
     (SELECT Geo, ... 
     FROM GeographicLocation t2 
     WHERE GeoId = t1.LocationId 
     FOR XML PATH('Location'), TYPE), 
     RtoId,      ^^^^ 
     ... 
FROM Project t1 
where ProjectId=1 
FOR XML PATH('ProjectInfo') 
+0

這是正確的答案 - 我把所有的舊代碼隱藏在我的最後一個公司,而這個答案讓我從拉我的頭髮。非常感謝! – edhubbell

1

將數據格式化爲xml,使用cast(@xml as xml)。

0

請嘗試:

(SELECT GeoId,PoliticalDivisionId ,GeographicLocationName,IsoCode, 
     Longitude,Latitude,ParentLocationId, 
     t2.CreatedBy,t2.CreatedOn,t2.LastUpdatedBy,t2.LastUpdatedOn 
    FROM GeographicLocation t2 
    WHERE GeoId = t1.LocationId 
     FOR XML PATH('Location'), type 
     ).value('(./text())[1]','varchar(max)')