C#对象序列化之坑

本文介绍了C#中XML序列化的一些注意事项,包括如何避免默认命名空间、解决UTF-8编码问题以及处理序列化结果开头的空白字符,以确保与其他系统交互时的兼容性。

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

        /// <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#,问题不大,但如果对方靠解析,就会出问题,因为前面那个空白字符,估计很多程序都没做自动滤掉处理。


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值