我试图插入XML列(SQL Server 2008 R2中),但服务器的抱怨:
System.Data.SqlClient.SqlException(0x80131904):
XML解析:1号线,39字符,无法切换编码
我发现XML列必须是UTF-16,以便插入成功。
在code我使用的是:
XmlSerializer的序列化=新的XmlSerializer(typeof运算(MyMessage));
StringWriter的海峡=新的StringWriter();
serializer.Serialize(STR,消息);
字符串messageToLog = str.ToString();
我如何序列化对象是在UTF-8字符串?
修改的:好了,遗憾的查询股价 - 字符串必须使用UTF-8。你是正确的 - 这是UTF-16在默认情况下,如果我尝试使用UTF-8将其插入通行证。所以,问题是如何序列化为UTF-8。
示例 这会导致错误,而试图插入SQL语句:
< XML版本=1.0编码=UTF-16&GT?;
< MyMessage>特诺< / MyMessage>
这不:
< XML版本=1.0编码=UTF-8&GT?;
< MyMessage>特诺< / MyMessage>
更新
我想通了,当SQLServer2008的为它的XML列类型需要UTF-8,当XML规范你想插入UTF-16 编码
属性
当你想添加UTF-8,然后将参数添加到SQL命令是这样的:
sqlcmd.Parameters.Add(PARAMNAME,SqlDbType.VarChar)。价值= xmlValueToAdd;
如果您尝试添加xmlValueToAdd与编码= UTF-16
的previous行,将在插入产生错误研究。此外,的VarChar
表示,国家字符不承认(他们变成问号)。
要添加UTF-16分贝,可以使用 SqlDbType.NVarChar
或 SqlDbType.Xml
在previous例如,或者只是不指定类型的所有:
sqlcmd.Parameters.Add(新的SqlParameter(PARAMNAME,xmlValueToAdd));
解决方案
虽然.NET字符串总是 UTF-16
则需要使用序列化对象 UTF-16
编码。
这是前人的精力是这样的:
公共静态字符串的ToString(对象来源,类型类型,编码编码)
{
//字符串来保存对象内容
字符串的内容;
//创建一个MemoryStream到其中的数据可以被写入和readed
使用(VAR流=新的MemoryStream())
{
//创建XML序列化,序列化需要知道的类型
//对象的将被序列
VAR XmlSerializer的=新的XmlSerializer(类型);
//创建一个XmlTextWriter中写的XML对象源,我们将
//定义构造函数中的编码
使用(VAR作家=新的XmlTextWriter(流,编码))
{
//保存对象的状态到流
xmlSerializer.Serialize(作家,源);
//刷新流
writer.Flush();
//读取流转换为字符串
使用(VAR读卡器=新的StreamReader(流,编码))
{
//设置流位置的开始
stream.Position = 0;
//读取流转换为字符串
含量= reader.ReadToEnd();
}
}
}
//返回XML字符串对象内容
返回的内容;
}
由编码设置为Encoding.Uni code不仅是字符串将 UTF-16
,但你也应该得到的XML字符串作为 UTF-16
。
< XML版本=1.0编码=UTF-16&GT?;
I'm trying to insert into XML column (SQL SERVER 2008 R2), but the server's complaining:
System.Data.SqlClient.SqlException (0x80131904):
XML parsing: line 1, character 39, unable to switch the encoding
I found out that the XML column has to be UTF-16 in order for the insert to succeed.
The code I'm using is:
XmlSerializer serializer = new XmlSerializer(typeof(MyMessage));
StringWriter str = new StringWriter();
serializer.Serialize(str, message);
string messageToLog = str.ToString();
How can I serialize object to be in UTF-8 string?
EDIT: Ok, sorry for the mixup - the string needs to be in UTF-8. You were right - it's UTF-16 by default, and if I try to insert in UTF-8 it passes. So the question is how to serialize into UTF-8.
Example This causes errors while trying to insert into Sql:
<?xml version="1.0" encoding="utf-16"?>
<MyMessage>Teno</MyMessage>
This doesn't:
<?xml version="1.0" encoding="utf-8"?>
<MyMessage>Teno</MyMessage>
Update
I figured out when the SqlServer2008 for it's Xml column type needs utf-8, and when utf-16 in encoding
property of the xml specification you're trying to insert:
When you want to add utf-8, then add parameters to sql command like this:
sqlcmd.Parameters.Add("ParamName", SqlDbType.VarChar).Value = xmlValueToAdd;
If you try to add the xmlValueToAdd with encoding=utf-16
in the previous row it would produce errors in insert. Also, the VarChar
means that national characters aren't recognized (they turn out as question marks).
To add utf-16 to db, either use SqlDbType.NVarChar
or SqlDbType.Xml
in previous example, or just don't specify type at all:
sqlcmd.Parameters.Add(new SqlParameter("ParamName", xmlValueToAdd));
解决方案
Although a .net string is always UTF-16
you need to serialize the object using UTF-16
encoding.
That sould be something like this:
public static string ToString(object source, Type type, Encoding encoding)
{
// The string to hold the object content
String content;
// Create a memoryStream into which the data can be written and readed
using (var stream = new MemoryStream())
{
// Create the xml serializer, the serializer needs to know the type
// of the object that will be serialized
var xmlSerializer = new XmlSerializer(type);
// Create a XmlTextWriter to write the xml object source, we are going
// to define the encoding in the constructor
using (var writer = new XmlTextWriter(stream, encoding))
{
// Save the state of the object into the stream
xmlSerializer.Serialize(writer, source);
// Flush the stream
writer.Flush();
// Read the stream into a string
using (var reader = new StreamReader(stream, encoding))
{
// Set the stream position to the begin
stream.Position = 0;
// Read the stream into a string
content = reader.ReadToEnd();
}
}
}
// Return the xml string with the object content
return content;
}
By setting the encoding to Encoding.Unicode not only the string will be UTF-16
but you should also get the xml string as UTF-16
.
<?xml version="1.0" encoding="utf-16"?>