将utf-8 XML文档转换为utf-16以插入到SQL中 [英] Convert utf-8 XML document to utf-16 for inserting into SQL
问题描述
我已经尝试使用XDocument这样做,但转换后我没有获得有效的XML结果。这是我已经尝试做的转换(Utf8StringWriter是一个继承自StringWriter和重载Encoding的小类):
XDocument xDoc = XDocument.Parse(utf8Xml);
StringWriter writer = new StringWriter();
XmlWriter xml = XmlWriter.Create(writer,new XmlWriterSettings()
{Encoding = writer.Encoding,Indent = true});
xDoc.WriteTo(xml);
string utf16Xml = writer.ToString();
utf16Xml中的数据无效,尝试插入数据库时会收到错误: / p>
{XML解析:第1行,字符38,无法切换编码}
然而,最初的utf8Xml数据绝对有效,并且包含我需要的所有信息。
UPDATE:
初始XML是通过使用XMLSerializer(使用Utf8StringWriter类)从现有对象模型(引擎)创建xml字符串获得的。这个代码是:
public static void Serialise&T;(T engine,ref StringWriter writer)
{
XmlWriter xml = XmlWriter.Create(writer,new XmlWriterSettings(){Encoding = writer.Encoding});
XmlSerializer xs = new XmlSerializer(engine.GetType());
xs.Serialize(xml,engine);
}
我不得不离开这样的代码是我的控制权更改。
在我甚至发送utf16Xml字符串到失败的数据库调用之前,我可以通过Visual Studio调试器查看它,我注意到整个字符串不存在
错误在于第一行 XDocument xDoc = XDocument.Parse(utf8Xml);
。很可能您将utf8流转换成字符串(utf8xml),但是字符串中指定的编码仍然是utf-8,因此XML阅读器失败。如果使用加载直接从流加载XML,而不是将其转换为第一个字符串。
I have an XML document that has been created using utf-8 encoding. I want to store that document in a sql 2008 xml column but I understand I need to convert it to utf-16 in order to do that.
I've tried using XDocument to do this but I'm not getting a valid XML result after the conversion. Here is what I've tried to do the conversion on (Utf8StringWriter is a small class that inherits from StringWriter and overloads Encoding):
XDocument xDoc = XDocument.Parse(utf8Xml);
StringWriter writer = new StringWriter();
XmlWriter xml = XmlWriter.Create(writer, new XmlWriterSettings()
{ Encoding = writer.Encoding, Indent = true });
xDoc.WriteTo(xml);
string utf16Xml = writer.ToString();
The data in the utf16Xml is invalid and when trying to insert into the database I get the error:
{"XML parsing: line 1, character 38, unable to switch the encoding"}
However the initial utf8Xml data is definitely valid and contains all the info I need.
UPDATE: The initial XML is obtained by using XMLSerializer (with an Utf8StringWriter class) to create the xml string from an existing object model (engine). The code for this is:
public static void Serialise<T>(T engine, ref StringWriter writer)
{
XmlWriter xml = XmlWriter.Create(writer, new XmlWriterSettings() { Encoding = writer.Encoding });
XmlSerializer xs = new XmlSerializer(engine.GetType());
xs.Serialize(xml, engine);
}
I have to leave this like this as that code is out of my control to change.
Before I even send the utf16Xml string to the failing database call I can view it via the Visual Studio debugger and I notice that the entire string is not present and instead I get a string literal was not closed error on the XML viewer.
The error is on first line XDocument xDoc = XDocument.Parse(utf8Xml);
. Most likely you converted utf8 stream into a string (utf8xml), but encoding specified in the string is still utf-8, so XML reader fails. If it is true than load XML directly from stream using Load instead of converting it to string first.
这篇关于将utf-8 XML文档转换为utf-16以插入到SQL中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!