Reading an UTF-16 encoded XML file using XmlSerializer

I call WebService and get the string returned from WebMethod. A string is an object serialized as XML that should be deserialized using System.Xml.XmlSerializer.

My problem is that the first line indicates that the document is UTF-16 encoded:

<?xml version="1.0" encoding="utf-16"?> 

Therefore, when deserializing, I get an error message:

 There is an error in XML document (0, 0). 

This works to execute string.Replace ("utf-16", "utf-8"), but should there be a clean method to know the XmlSerializer?

+4
source share
1 answer

This should not affect anything: the following works fine:

 using System; using System.IO; using System.Xml.Serialization; [XmlRoot("someType")] public class Test { [XmlAttribute("hello")] public string Value { get; set; } } static class Program { static void Main() { string xml = @"<?xml version=""1.0"" encoding=""utf-16""?> <someType hello=""world""/>"; var ser = new XmlSerializer(typeof(Test)); Test obj; using (var reader = new StringReader(xml)) { obj = (Test)ser.Deserialize(reader); } Console.WriteLine(obj.Value); } } 
+5
source

All Articles