c# - Convert utf-8 XML document to utf-16 for inserting into SQL -


i have xml document has been created using utf-8 encoding. want store document in sql 2008 xml column understand need convert utf-16 in order that.

i've tried using xdocument i'm not getting valid xml result after conversion. here i've tried conversion on (utf8stringwriter small class inherits stringwriter , 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 utf16xml invalid , when trying insert database error:

{"xml parsing: line 1, character 38, unable switch encoding"} 

however initial utf8xml data valid , contains info need.

update: initial xml obtained using xmlserializer (with utf8stringwriter class) create xml string existing object model (engine). code 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 leave code out of control change.

before send utf16xml string failing database call can view via visual studio debugger , notice entire string not present , instead string literal not closed error on xml viewer.

the error on first line xdocument xdoc = xdocument.parse(utf8xml);. converted utf8 stream string (utf8xml), encoding specified in string still utf-8, xml reader fails. if true load xml directly stream using load instead of converting string first.


Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -