Recently we had to save a class as xml in SQL Server and then restore it. Turns out the default type was UTF-8 and the SQL Server xml type uses UTF-16, so it did not work.

Then i found this:

Its vb, but heres some c# that also changes the encoding:

if (doc.FirstChild.NodeType == XmlNodeType.XmlDeclaration)

// Get the encoding declaration.

XmlDeclaration decl = (XmlDeclaration)


// Set the encoding declaration.

decl.Encoding = “UTF-16”;



Use this code after you have serialized the class to xml. Then use this and save your class.

Latest posts by Ken Spencer (see all)