Salut je suis nouveau pour xml
J'ai une requête comme celle-ci
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')
il retourne le xml en tant que
<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>
Les balises prject apparaissent sous la forme <et>. Mais les balises internes de Location sont indiquées par “<” et “>”.
Mise à jour : il y avait une petite erreur dans la question. xml interne n'était pas pour roid, c'était pour Location
J'ai mis à jour la requête en tant que
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') ), '<', '<'), '>', '>'),
RtoId,
CreatedBy,
CreatedOn,
LastUpdatedBy,
LastUpdatedOn
FROM Project t1
where ProjectId=1
FOR XML PATH('ProjectInfo')
mais c'est toujours pareil
Je pense que la manière correcte utilise Directive TYPE
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')
La façon dont j'ai trouvé est de les remplacer explicitement:
select ProjectId, ProjectCode, ProjectName, TechId,
replace(replace(RtoId, '<', '<'), '>', '>') 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') ), '<', '<'), '>', '>')
RtoId,
CreatedBy,
CreatedOn,
LastUpdatedBy,
LastUpdatedOn
FROM Project t1
where ProjectId=1
FOR XML PATH('ProjectInfo')
formatez les données en XML, utilisez cast (@xml en tant que 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')
S'il vous plaît essayez:
(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)')