IT博客汇
  • 首页
  • 精华
  • 技术
  • 设计
  • 资讯
  • 扯淡
  • 权利声明
  • 登录 注册

    [原]C#对象序列化之坑

    hawksoft发表于 2017-01-06 17:03:07
    love 0

    序列化的方法很简单,如下:

            /// <summary>
            /// 文本化XML序列化
            /// </summary>
            /// <param name="Obj">对象</param>
            public static string ToXml1<T>(T Obj)
            {
                XmlSerializerNamespaces theNames = new XmlSerializerNamespaces();
                theNames.Add("", "");
                XmlSerializer theSerializer = new XmlSerializer(Obj.GetType());
                StringBuilder theSB = new StringBuilder();
                var theXmlWS = new XmlWriterSettings();
                theXmlWS.Encoding = Encoding.UTF8;
                var theXml = "";
                using (XmlWriter writer = XmlWriter.Create(theSB, theXmlWS))
                {
                    theSerializer.Serialize(writer, Obj, theNames);
                    theXml = theSB.ToString();
                }
                return theXml;
            }

    注意,如果不加空的命名空间,序列化的时候会加一些默认的名字空间,非常不好;但这个方法有个问题,就是输出的是utf-16,中间那个设置为utf8没鸟用,为了序列化成utf-8,如下写:

    /// <summary>
            /// 文本化XML序列化
            /// </summary>
            /// <param name="Obj">对象</param>
            public static string ToXml<T>(T Obj, Encoding TextEncoding = null)
            {
                if (TextEncoding == null)
                {
                    TextEncoding = Encoding.UTF8;
                }
                XmlSerializerNamespaces theNames = new XmlSerializerNamespaces();
                theNames.Add("", "");

                MemoryStream theMS = new MemoryStream();
                StreamWriter theTextWriter = new StreamWriter(theMS, TextEncoding);
                XmlSerializer theSerializer = new XmlSerializer(Obj.GetType());
                var theXml = "";
                using (XmlWriter writer = XmlWriter.Create(theTextWriter))
                {
                    theSerializer.Serialize(writer, Obj, theNames);
                    var theDataBytes = theMS.GetBuffer();
                    theXml = TextEncoding.GetString(theDataBytes);
                }
                theTextWriter.Close();
                theMS.Close();
                return theXml;
            }

    上面代码序列化正常,但IE浏览器可能对上述结果无法正常显示,经过调试我发现,序列化时在头部会多一个空白字符,经过修改,如下才是正常:

    /// <summary>
            /// 文本化XML序列化
            /// </summary>
            /// <param name="Obj">对象</param>
            public static string ToXml<T>(T Obj, Encoding TextEncoding = null)
            {
                if (TextEncoding == null)
                {
                    TextEncoding = Encoding.UTF8;
                }
                XmlSerializerNamespaces theNames = new XmlSerializerNamespaces();
                theNames.Add("", "");

                MemoryStream theMS = new MemoryStream();
                StreamWriter theTextWriter = new StreamWriter(theMS, TextEncoding);
                XmlSerializer theSerializer = new XmlSerializer(Obj.GetType());
                var theXml = "";
                using (XmlWriter writer = XmlWriter.Create(theTextWriter))
                {
                    theSerializer.Serialize(writer, Obj, theNames);
                    var theDataBytes = theMS.GetBuffer();
                    theXml = TextEncoding.GetString(theDataBytes);
                }
                theTextWriter.Close();
                theMS.Close();
                var thePos = theXml.IndexOf("<");
                return theXml.Substring(thePos);
            }

    在跟对方做接口时发现,如果序列化和反序列化都是C#,问题不大,但如果对方靠解析,就会出问题,因为前面那个空白字符,估计很多程序都没做自动滤掉处理。

    


沪ICP备19023445号-2号
友情链接