serialization - 为什么单点触控中模拟器和 iPad 设备的序列化行为不同?

标签 serialization xamarin.ios

为什么单点触控中模拟器和 iPad 设备的序列化行为不同? 我已声明序列化行为为 UTF-8,但是当将序列化数据从 ipad 设备发送到 WCF 服务时,我跟踪消息,它更改为 ascii,为什么? (在模拟器中使用 UTF-8 就可以了)

模拟器/中文中的跟踪: < ?xml 版本=“1.0”编码=“utf-8”? > ...

在iPad设备/中文中跟踪: < ?xml 版本=“1.0”编码=“us-ascii”? > ...

顺便说一句,我使用静态方法手动序列化数据:XmlSerialize。

    public static string XmlSerialize<T>(T obj)
    {
        using (MemoryStream ms = new MemoryStream())
        {
            XmlSerializer serializer = new XmlSerializer(typeof(T));
            serializer.Serialize(ms, obj);
            ms.Seek(0, SeekOrigin.Begin);
            using (StreamReader reader = new StreamReader(ms, Encoding.UTF8))
            {
                return reader.ReadToEnd();
            }
        }
    }


    public static T XmlDeserialize<T>(string xmlOfObject) where T : class
    {
        using (MemoryStream ms = new MemoryStream())
        {
            using (StreamWriter sr = new StreamWriter(ms, Encoding.UTF8))
            {
                sr.Write(xmlOfObject);
                sr.Flush();
                ms.Seek(0, SeekOrigin.Begin);
                XmlSerializer serializer = new XmlSerializer(typeof(T));
                return serializer.Deserialize(ms) as T;
            }
        }
    }

最佳答案

您还需要设置 XmlSerializer 才能使用 utf-8:

var serializer = new XmlSerializer(typeof(MyClass));

using (var writer = new StreamWriter("file.path"))
{
  using (var xmlWriter = XmlWriter.Create(writer, new XmlWriterSettings { Indent = false, Encoding = Encoding.UTF8 }))
  {
    serializer.Serialize(xmlWriter, myObject);
  }
}

否则它将使用默认编码,无论发生什么。

关于serialization - 为什么单点触控中模拟器和 iPad 设备的序列化行为不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12912318/

相关文章:

java - 在序列化期间忽略 Jackson JsonIdentityInfo

ios5 - "Application tried to present modally an active controller"当用户下拉通知中心时

ios - 您可以使用 MonoTouch 使用 Kerberos 身份验证连接到 ASP.net soap 服务吗?

xamarin - 绑定(bind) iOS 框架会导致临时文件损坏

java - Gson 自定义类型适配器仅在一种对象类型上序列化 null?

python - 将计数字段添加到 django rest 框架序列化程序

iOS6:Twitter SetInitialText 不起作用

ios - 更新 Xcode 后的 EXC_BAD_ACCESS (SIGABRT)

java - JSONTypeInfo 不忽略继承映射中的属性

c# - JSON.NET 序列化对象,其中属性名称以点开头