嗨,我是xml的新手
我有这样的查询
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> <Location><GeoId>235</GeoId><PoliticalDivisionId>2</PoliticalDivisionId><GeographicLocationName>UNITED STATES</GeographicLocationName><IsoCode>US</IsoCode></Location> <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>
项目标签以<和> .但是位置的内部标签显示为“<”和“>”如何用<和>
更新:问题中有一个小错误.内部xml不适用于rtoid,它是针对位置
我更新了查询
SELECT ProjectId,replace(replace( ( SELECT GeoId,'<','<'),'>','>'),LastUpdatedOn FROM Project t1 where ProjectId=1 FOR XML PATH('ProjectInfo')
但仍然是一样的
解决方法
我认为正确的方法是使用
TYPE Directive
SELECT ProjectId,...,( SELECT Geo,... FROM GeographicLocation t2 WHERE GeoId = t1.LocationId FOR XML PATH('Location'),TYPE),^^^^ ... FROM Project t1 where ProjectId=1 FOR XML PATH('ProjectInfo')