将 XML 插入 SQL Server 2008 数据库 [英] INSERT XML into SQL Server 2008 database
问题描述
您好,我正在尝试将一些 XML 数据插入 SQL Server 2008 上的表中.但是我不断收到此错误;
Hello I'm trying to insert some XML data into a table on SQL Server 2008. However I keep getting thrown this error;
XML 解析:第 1 行,第 39 个字符,无法切换编码
XML parsing: line 1, character 39, unable to switch the encoding
数据库列 filemeta 使用 XML 数据类型,我已将编码切换为 UTF-16,我认为这是添加 XML 数据所必需的.
The database column filemeta uses the XML datatype, and I've switch the encoding to UTF-16 which I believe is necessary for adding XML data.
INSERT INTO testfiles
(filename, filemeta)
VALUES
('test.mp3', '<?xml version="1.0" encoding="utf-16" standalone="yes"?><!--This is a test XML file--><filemeta filetype="Audio"><Comments /><AlbumTitle /><TrackNumber /><ArtistName /><Year /><Genre /><TrackTitle /></filemeta>');
帮助,我被卡住了.
注意:我使用 XMLTextWriter 创建了 XML.
NB: I created the XML with XMLTextWriter.
推荐答案
是的,当您尝试将 XML 插入 SQL Server 2008 并且 XML 包含编码指令行时会出现问题.
Yes, there are issues when you try to insert XML into SQL Server 2008 and the XML contains an encoding instruction line.
我通常会使用 CONVERT
函数,它允许我指示 SQL Server 跳过这些指令 - 使用如下所示:
I typically get around using the CONVERT
function which allows me to instruct SQL Server to skip those instructions - use something like this:
INSERT INTO testfiles
(filename, filemeta)
VALUES
('test.mp3', CONVERT(XML, N'<?xml version="1.0" encoding="utf-16" standalone="yes"?>......', 2));
它确实帮助我将各种编码的 XML 内容导入 SQL Server.
It has definitely helped me get various encoded XML stuff into SQL Server.
请参阅 有关 CAST 和 CONVERT 的 MSDN 文档 - 有点低页面中有许多样式可以用于 CONVERT
和 XML
以及有关它们的一些解释.
See the MSDN docs on CAST and CONVERT - a bit down the page there's a number of styles you can use for CONVERT
with XML
and some explanations about them.
这篇关于将 XML 插入 SQL Server 2008 数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!