简体   繁体   English

尝试将XML内容存储到SQL Server 2005失败(编码问题)

[英]Trying to store XML content into SQL Server 2005 fails (encoding problem)

Folks, 伙计们,

I have a webservice that returns data in ISO-8859-1 encoding - since it's not mine, I can't change that :-( 我有一个web服务,返回ISO-8859-1编码的数据 - 因为它不是我的,我不能改变:-(

For auditing purposes, I'd like to store the resulting XML from these calls into a SQL Server 2005 table, in which I have a field of type "XML NULL". 出于审计目的,我想将这些调用生成的XML存储到SQL Server 2005表中,其中我有一个类型为“XML NULL”的字段。

From my C# code, I try to store this XML content into the XML field using a parametrized query, something like 从我的C#代码中,我尝试使用参数化查询将此XML内容存储到XML字段中,例如

SqlCommand _cmd = new SqlCommand("INSERT INTO dbo.AuditTable(XmlField) VALUES(@XmlContents)", _connection);

_cmd.Parameters.Add("@XmlContents", SqlDbType.Xml);
_cmd.Parameters["@XmlContents"].Value = (my XML response);

_cmd.ExecuteNonQuery();

Trouble is - when I run this code, I get back an error: 麻烦的是 - 当我运行此代码时,我收到一个错误:

Msg 9402, Level 16, State 1, Line 1 Msg 9402,Level 16,State 1,Line 1
XML parsing: line 1, character xy, unable to switch the encoding XML解析:第1行,字符xy,无法切换编码

?? ?? I was trying to figure out where and how I could possibly "switch" the encoding - no luck so far. 我试图弄清楚我可以在哪里以及如何“切换”编码 - 到目前为止没有运气。 What does this really mean? 这究竟意味着什么? I cannot store XML with ISO-8859-1 encoding in SQL Server 2005?? 我无法在SQL Server 2005中使用ISO-8859-1编码存储XML? Or is there a trick to a) tell SQL Server 2005 to just accept this encoding, or b) to automagically convert the webservice response to UTF encoding before storing in SQL Server? 或者有一个技巧:a)告诉SQL Server 2005只接受这种编码,或b)在存储到SQL Server之前自动将webservice响应转换为UTF编码?

Thanks for any hints, pointers, tips! 感谢任何提示,指示,提示! Marc

You need to convert to utf-16 你需要转换为utf-16

I'm not an expert on XML in SQL Server even though I use it, but we had the same problem last year and it was mis-match of the string datatype declared in SQL compared to the xml being sent. 即使我使用它,我也不是SQL Server中的XML专家,但去年我们遇到了同样的问题,而且与发送的xml相比,SQL中声明的字符串数据类型不匹配。

Edit 编辑
I missed the ISO-8859-1 part of the question - the solution below is good for UTF8, but obviously doesn't solve Marc's problem as he can't alter the encoding. 我错过了问题的ISO-8859-1部分 - 下面的解决方案对UTF8有好处,但显然不能解决Marc的问题,因为他无法改变编码。


Here's the solution I use: 这是我使用的解决方案:

And a slightly modified version of the code from above (I've tested it with a UTF8 file using SQL 2005): 以及上面代码的略微修改版本(我使用SQL 2005使用UTF8文件对其进行了测试):

using System.IO;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;

...
using (SqlConnection connection = new SqlConnection("conn string"))
{
    connection.Open();
    string sql = "INSERT INTO mytable (xmlColumn) VALUES (@xmlData)";
    using (SqlCommand command = new SqlCommand(sql, connection))
    {
        // Swap round if the source file is unicode         
        string xml = File.ReadAllText(@"C:\myxml.xml");
        //string xml = File.ReadAllText(@"C:\myxml.xml", Encoding.Unicode);

        using (MemoryStream stream = new MemoryStream())
        {
            using (StreamWriter writer = new StreamWriter(stream, Encoding.Unicode))
            {
                writer.Write(xml);
                writer.Flush();
                stream.Position = 0;

                SqlParameter parameter = new SqlParameter("@xmlData", SqlDbType.Text);
                parameter.Value = new SqlXml(stream);
                command.Parameters.Add(parameter);
                command.ExecuteNonQuery();
            }
        }
    }
}

Even I faced similar issue while inserting xml content to db. 即使我在向db中插入xml内容时遇到了类似的问题。 For ex , input was like this: 例如,输入是这样的:

Insert Into TestData(Xml) Values ('<?xml version="1.0" encoding="UTF-8"?><Test/>')

This kind of statement used to fail and I was getting "unable to switch .." error. 这种语句曾经失败,我得到“无法切换..”的错误。 Later I simply prefixed N to xml string like this : 后来我简单地将N加到xml字符串中,如下所示:

Insert Into TestData(Xml) Values (N'<?xml version="1.0" encoding="UTF-8"?><Test/>')

After this it started working !!! 在此之后它开始工作!

I found this on google. 我在谷歌上发现了这个。 http://social.msdn.microsoft.com/forums/en-US/sqlxml/thread/d40ef582-4ffe-4f4b-b6b8-03c6c0ba1a32/ http://social.msdn.microsoft.com/forums/en-US/sqlxml/thread/d40ef582-4ffe-4f4b-b6b8-03c6c0ba1a32/

I think you can replace the line 我想你可以换掉这条线

_cmd.Parameters.Add("@XmlContents", SqlDbType.Xml);

with

_cmd.Parameters.Add("@XmlContents", System.Data.SqlTypes.SqlXml);

Could you possibly re-write the xml as unicode (perhaps to a MemoryStream ) and send that? 您是否可以将xml重新编写为unicode(可能是一个MemoryStream )并发送它? Note: if you are just storing the data, you can use varbinary(max) (and it will actually be quicker). 注意:如果您只是存储数据,则可以使用varbinary(max) (实际上它会更快)。 This has no encoding difficulties, and will also allow you to audit any corrupt xml that you receive. 这没有编码困难,并且还允许您审核您收到的任何损坏的xml。

If you are querying the data as xml inside the database server then xml is obviously the way to go. 如果你在数据库服务器中查询数据为xml,那么xml显然是要走的路。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM