I'm facing a problem to parse an XML string stored in a field of type NVARCHAR(MAX)
(I cannot change the type of this field).
Here is my table (WorkingHours) :
CREATE TABLE WorkingHours(
[ID] [int] NOT NULL PRIMARY KEY,
[CONTENT] [nvarchar](MAX) NOT NULL,
-- ...
);
Here is a sample of the [CONTENT]
attribute :
<?xml version="1.0" encoding="UTF-8"?>
<calendar>
<day number="1" worked_day="no">
<interval number="1" begin_hour="08:30" end_hour="12:00"/>
<interval number="2" begin_hour="13:30" end_hour="17:00"/>
<interval number="3" begin_hour="" end_hour=""/></day>
<day number="2" worked_day="no">
<interval number="1" begin_hour="08:30" end_hour="12:00"/>
<interval number="2" begin_hour="13:30" end_hour="17:00"/>
<interval number="3" begin_hour="" end_hour=""/>
</day>
<day number="3" worked_day="no">
<interval number="1" begin_hour="08:30" end_hour="12:00"/>
<interval number="2" begin_hour="13:30" end_hour="17:00"/>
<interval number="3" begin_hour="" end_hour=""/>
</day>
<day number="4" worked_day="no">
<interval number="1" begin_hour="08:30" end_hour="12:00"/>
<interval number="2" begin_hour="13:30" end_hour="17:00"/>
<interval number="3" begin_hour="" end_hour=""/>
</day>
<day number="5" worked_day="no">
<interval number="1" begin_hour="08:30" end_hour="12:00"/>
<interval number="2" begin_hour="13:30" end_hour="17:00"/>
<interval number="3" begin_hour="" end_hour=""/>
</day>
<day number="6" worked_day="no">
<interval number="1" begin_hour="" end_hour=""/>
<interval number="2" begin_hour="" end_hour=""/>
<interval number="3" begin_hour="" end_hour=""/>
</day>
<day number="7" worked_day="no">
<interval number="1" begin_hour="" end_hour=""/>
<interval number="2" begin_hour="" end_hour=""/>
<interval number="3" begin_hour="" end_hour=""/>
</day>
</calendar>
As you can see, the data encoding is UTF-8 .
Now, I would like to parse this data in order to create some calculations :
DECLARE @RawContent [nvarchar](MAX) = (
SELECT wh.[CONTENT]
FROM [WorkingHours] wh
WHERE wh.[ID] = 100);
DECLARE @XMLContent [Xml] = @RawContent; // KO
-- DECLARE @XMLContent [Xml] = CAST(@RawContent AS XML); // KO
-- DECLARE @XMLContent [Xml] = CONVERT(XML, @RawContent); // KO
-- Just a test to query XML data.
SELECT
C.WD.value('@number', 'int') AS DayId
FROM @XMLContent.nodes('/calendar/day') AS C(WD);
I don't know how to cast the result (a nvarchar(max) field containing UTF-8 XML string) to a XML value. SQL Server returns the following error :
"Unable to switch encoding"
It refers to the CAST line (when I define the @XMLContent variable).
Any idea to solve that ?
Strip out the processing directive -- it's meaningless and incorrect because the data is already encoded in UTF-16 (since it's stored as NVARCHAR
). If you cannot change the data already present, you'll have to rely on (slightly brittle) string replacement:
CAST(REPLACE(wh.[CONTENT], '<?xml version="1.0" encoding="UTF-8"?>', '') AS XML)
Note that explicitly indicating the encoding is UTF-16 instead will also work -- though it adds nothing.
The other option is to convert to a VARCHAR
datatype first - which is non-Unicode - and then to XML
:
DECLARE @RawContent [nvarchar](MAX) = (
SELECT wh.[CONTENT]
FROM [WorkingHours] wh
WHERE wh.[ID] = 100);
DECLARE @XMLContent XML = CAST(CAST(@RawContent AS VARCHAR(MAX)) AS XML)
-- Just a test to query XML data.
SELECT
C.WD.value('@number', 'int') AS DayId
FROM @XMLContent.nodes('/calendar/day') AS C(WD);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.