第一次连载成功,第二次失败?为什么?

First serialization success, second fails? Why?

我有一种序列化通信数据的方法。首先将参数作为字符串序列化到主 CallInformation 对象中,然后 CallInformation 对象也将被序列化,然后写入文件:

_jSONSettings = new System.Runtime.Serialization.Json.DataContractJsonSerializerSettings();
            _jSONSettings.DateTimeFormat = new DateTimeFormat("yyyy-MM-ddThh:mm:ss.fffZ");

            _xmlWriterSettings = new System.Xml.XmlWriterSettings() { Indent = true };

var callInformation = logEvent.Properties.Values.First() as CallInformation;

            DataContractJsonSerializer serializer;
            if (TakeCharsInParameterObject > 0)
            {
                var counter = 0;
                foreach (object param in callInformation.Parameters)
                {
                    using (var stream = new MemoryStream())
                    {
                        serializer = new DataContractJsonSerializer(param.GetType(), _jSONSettings);
                        using (var xmlWriter = System.Xml.XmlWriter.Create(stream, _xmlWriterSettings))
                        {
                            serializer.WriteObject(xmlWriter, param);
                            stream.Flush();
                            stream.Position = SkipCharsInParameterObject;
                            using (var streamReader = new StreamReader(stream))
                            {
                                var buffer = new char[TakeCharsInParameterObject];
                                if (streamReader.Peek() >= 0)
                                {
                                    streamReader.Read(buffer, 0, buffer.Length);
                                    counter++;
                                    callInformation.SerializedParameters += "{Parameter" + counter + ": " + new string(buffer) + "}";
                                }
                            }
                        }
                    }
                }
            }


            var restult = "";
            using (var stream = new MemoryStream())
            {
                serializer = new DataContractJsonSerializer(typeof(CallInformation), _jSONSettings);
                using (var xmlWriter = System.Xml.XmlWriter.Create(stream, _xmlWriterSettings))
                {
                    serializer.WriteObject(xmlWriter, callInformation);
                    stream.Flush();
                    stream.Position = 0;
                    using (var streamReader = new StreamReader(stream))
                    {
                        if (streamReader.Peek() >= 0)
                            restult = streamReader.ReadToEnd();
                    }
                }
            }
            return restult;

参数的序列化效果很好,但第二部分没有。 stream.WriteObject 之后流的长度和位置仍然是 0?

CallInformation 是一个简单的 DataContract class,如下所示:

   [DataContract]
    public class CallInformation
    {
        public CallInformation()
        { }

        [DataMember]
        public string Address { get; set; }
        [DataMember]
        public Boolean IsEmpty { get; set; }
        [DataMember]
        public Boolean IsFaulted { get; set; } = false;
        [DataMember]
        public string Action { get; set; }
        [DataMember]
        public CallOrder CallDirection { get; set; }

        public DateTime EventTime { get; set; } = DateTime.Now;
        [DataMember]
        public TimeSpan Duration { get; set; }
        [DataMember]
        public Boolean IsCallback { get; set; } = false;
        [DataMember]
        public string LogSource { get; set; } = "Unknown";
        [DataMember]
        public string SerializedParameters { get; set; } = "";

        public List<object> Parameters { get; set; } = new List<object>();
        [DataMember]
        public string EventTimeDisplay
        {
            get { return EventTime.ToString("HH:mm:ss.fffffff"); }
            set { }
        }
    }

为什么第二个序列化不起作用?

XmlWriter 有内部缓冲区,您需要刷新它以更改流位置:

serializer.WriteObject(xmlWriter, callInformation);
xmlWriter.Flush();
stream.Position = 0;