I am writing a simple powershell script to insert XML files from a folder into SQL Server 2012. But every time it's failing when trying to insert the XML with below error. In Microsoft documentation, they are inserting UTF-8 encoded xml. I am able to insert XML data when I am changing encoding manually to "UTF-16". What I need to do to inset the XML without changing UTF-8 encoding. Thanks.
Error:
Exception calling "ExecuteScalar" with "0" argument(s): "XML parsing: line 1, character 38, unable to switch the encoding"
Line
$InsertedID = $sqlCommand.ExecuteScalar()
Table Definition:
CREATE TABLE TESTXML
(
ID INT IDENTITY(1,1) NOT NULL,
FileName VARCHAR(200),
InsertedDate DATETIME DEFAULT GETDATE(),
InsertedBy VARCHAR(100) DEFAULT 'PS',
XML XML,
BLOB VARBINARY(MAX)
)
XML to parse:
<?xml version="1.0" encoding="UTF-8"?>
<order xmlns="http://XXXXX/xml/impex/XXX/2006-10-31" order-no="XXX">
XML Data......
</order>
Powershell Script:
foreach($xmlfile in $xmlfiles){
[string]$xml = (Get-Content $xmlfile.FullName) -replace "'", "''"
$sqlCommand.Parameters[0].Value = $xmlfile.FullName
$sqlCommand.Parameters[1].Value = $xml
$InsertedID = $sqlCommand.ExecuteScalar()
"Inserted row ID $InsertedID for file " + $xmlfile.FullName
}