Question

Hi I am new to for xml

I have a query like this

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')

it return the xml as

<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 tags are shown in the form < and > . But inner tags of Location are shown as “<” and “>” how do I replace them with < and >

Update : there was a small error in the question . inner xml was not for rtoid , it was for Location

I updated query as

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')

but still its the same

No correct solution

OTHER TIPS

I think correct way is using 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') 

The way that I've found is with explicitly replacing them:

select ProjectId, ProjectCode, ProjectName, TechId,
       replace(replace(RtoId, '&lt;', '<'), '&gt;', '>') as RtoId, 
       . . .
from (<your query here>)
    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')

Please try:

(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)')

format the data into xml, use cast(@xml as xml).

SELECT ProjectId,
       ProjectCode, 
       ProjectName, 
       TechId, 
      -- LocationId, 
      cast(( 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') ) as xml),
       RtoId, 
       CreatedBy,
       CreatedOn,
       LastUpdatedBy,
       LastUpdatedOn
FROM Project t1
where ProjectId=1
FOR XML PATH('ProjectInfo')

Use FOR XML

SELECT customerid 
  , name
  , SUBSTRING( x, 4, LEN( x) - 7)        AS name2
  , IIF( LEN(name) <> LEN(x) - 7, 1, 0)  AS residual 
FROM (SELECT customerid
        ,   name
        ,   (SELECT kundnamn AS n FOR XML PATH('')) as x
      FROM customers
      ) s

or

declare @s varchar(MAX) = 'asd& < > " '' ='
PRINT @s
declare @xml varchar(MAX)
SELECT @xml = SUBSTRING( x, 4, LEN( x) - 7)
FROM (SELECT (SELECT @s AS x FOR XML PATH('')) AS x ) x
PRINT @xml

gives

asd& < > " ' =
asd&amp; &lt; &gt; " ' =
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top