Question

I have a string of XML(utf-8).I need to store the string in the database(MS SQL). Encoding a string must be UTF-16.

This code does not work, utf16Xml is 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 a serialize object(encoding UTF8).

How convert xml string UTF8 to UTF16?

Was it helpful?

Solution

This might help 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);

OTHER TIPS

Given that XDocument.Parse only accepts a string, and that string in .NET is always UTF-16 Little Endian, it looks like you are going through a lot of steps to effectively do nothing. Either:

  1. The string – utf8Xml – is already UTF-16 LE and can be inserted into SQL Server as is (i.e. do nothing) as SqlDbType.Xml or SqlDbType.NVarChar,

    or
     

  2. utf8Xml somehow contains UTF-8 byte sequences, which would be invalid UTF-16 LE (i.e. "Unicode" in Microsoft-land) byte sequences. If this is the case, then you might be able to simply:
    1. add the XML Declaration, stating that the encoding is UTF-8:
      xDoc.Declaration.Encoding = "utf-8";
    2. do not omit the XML declaration:
      OmitXmlDeclaration = false;
    3. pass utf8Xml into SQL Server as DbType.VarChar

For further explanation, please see my answer to the related question (here on S.O.):

How to solve “unable to switch the encoding” error when inserting XML into SQL Server

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top