Home Forums Programming XML Building XML and controlling Nodes RE: Building XML and controlling Nodes

  • If you are going to switch the encoding from UTF-16 to Windows-1252, you will have to work with the final output as NVARCHAR(MAX) as it cannot be cast back to SQL Server XML data type.

    AdventureWorks2012 sample:

    DECLARE @XML XML = N'';

    DECLARE @XMLSTR NVARCHAR(MAX) = N'';

    SELECT @XML = (

    SELECT

    SDH.SalesOrderID AS '@SalesOrderID'

    ,(SELECT

    SD.OrderQty AS 'Product/@OrderQty'

    ,SD.CarrierTrackingNumber AS 'Product/ProductID/SalesOrderDetailID/@CarrierTrackingNumber'

    ,SD.ProductID AS 'Product/ProductID'

    ,SD. SalesOrderDetailID AS 'Product/ProductID/SalesOrderDetailID'

    FROM Sales.SalesOrderDetail SD

    WHERE SD.SalesOrderID = SDH.SalesOrderID

    FOR XML PATH(''),TYPE) AS Products

    FROM Sales.SalesOrderHeader SDH

    WHERE SDH.SalesOrderID = 44116

    FOR XML PATH('SalesOrder'),TYPE, ROOT('SalesOrders'));

    select @XMLSTR = N'<?xml version = "1.0" encoding="Windows-1252" standalone="yes"?>

    <VFPDataSet xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="C:\PHC15\AddTableToSchema_Nest.xsd">' + CAST(@XML AS NVARCHAR(MAX))

    Also, the Schema Location should not be a local path, xsi:noNamespaceSchemaLocation="C:\PHC15\AddTableToSchema_Nest.xsd"

    😎