当我以前遇到过这种情况时,它通常与编码有关.我在序列化对象时尝试指定编码.尝试使用以下代码.另外,是否有任何特定原因需要返回string[]
数组?我已经改变了使用泛型的方法,因此您不必指定类型.
private static string SerializeObjectToXmlString(T obj) { XmlSerializer xmls = new XmlSerializer(typeof(T)); using (MemoryStream ms = new MemoryStream()) { XmlWriterSettings settings = new XmlWriterSettings(); settings.Encoding = Encoding.UTF8; settings.Indent = true; settings.IndentChars = "\t"; settings.NewLineChars = Environment.NewLine; settings.ConformanceLevel = ConformanceLevel.Document; using (XmlWriter writer = XmlTextWriter.Create(ms, settings)) { xmls.Serialize(writer, obj); } string xml = Encoding.UTF8.GetString(ms.ToArray()); return xml; } } private static T DeserializeXmlStringToObject (string xmlString) { XmlSerializer xmls = new XmlSerializer(typeof(T)); using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(xmlString))) { return (T)xmls.Deserialize(ms); } }
如果您仍然遇到问题,请尝试Encoding.ASCII
在您看到的任何地方使用您的代码Encoding.UTF8
,除非您有使用UTF8的特定原因.我不确定原因,但在序列化的某些情况下,我看到UTF8编码导致了这个确切的问题.
当我以前遇到过这种情况时,它通常与编码有关.我在序列化对象时尝试指定编码.尝试使用以下代码.另外,是否有任何特定原因需要返回string[]
数组?我已经改变了使用泛型的方法,因此您不必指定类型.
private static string SerializeObjectToXmlString(T obj) { XmlSerializer xmls = new XmlSerializer(typeof(T)); using (MemoryStream ms = new MemoryStream()) { XmlWriterSettings settings = new XmlWriterSettings(); settings.Encoding = Encoding.UTF8; settings.Indent = true; settings.IndentChars = "\t"; settings.NewLineChars = Environment.NewLine; settings.ConformanceLevel = ConformanceLevel.Document; using (XmlWriter writer = XmlTextWriter.Create(ms, settings)) { xmls.Serialize(writer, obj); } string xml = Encoding.UTF8.GetString(ms.ToArray()); return xml; } } private static T DeserializeXmlStringToObject (string xmlString) { XmlSerializer xmls = new XmlSerializer(typeof(T)); using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(xmlString))) { return (T)xmls.Deserialize(ms); } }
如果您仍然遇到问题,请尝试Encoding.ASCII
在您看到的任何地方使用您的代码Encoding.UTF8
,除非您有使用UTF8的特定原因.我不确定原因,但在序列化的某些情况下,我看到UTF8编码导致了这个确切的问题.