c# - How convert xml string UTF8 to UTF16? -
i have string of xml(utf-8).i need store string in database(ms sql). encoding string must utf-16.
this code not work, utf16xml empty
xdocument xdoc = xdocument.parse(utf8xml); xdoc.declaration.encoding = "utf-16"; stringwriter writer = new stringwriter(); xmlwriter xml = xmlwriter.create(writer, new xmlwritersettings() { encoding = writer.encoding, indent = true }); xdoc.writeto(xml); string utf16xml = writer.tostring();
utf8xml - string contains serialize object(encoding utf8).
how convert xml string utf8 utf16?
this might you
memorystream ms = new memorystream(); xmlwritersettings xws = new xmlwritersettings(); xws.omitxmldeclaration = true; xws.indent = true; xdocument xdoc = xdocument.parse(utf8xml); xdoc.declaration.encoding = "utf-16"; using (xmlwriter xw = xmlwriter.create(ms, xws)) { xdoc.writeto(xw); } encoding ut8 = encoding.utf8; encoding ut116 = encoding.unicode; byte[] utf16xmlarray = encoding.convert(ut8, ut116, ms.toarray()); var utf16xml = encoding.unicode.getstring(utf16xmlarray);
Comments
Post a Comment