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

问题描述 投票:0回答:1

我有一个序列化通信数据的方法。首先参数被序列化为字符串进入主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类,看起来像这样。

   [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 { }
        }
    }

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

c# .net json serialization datacontract
1个回答
0
投票

XmlWriter有内部缓冲区,你需要刷新它来改变流的位置。

serializer.WriteObject(xmlWriter, callInformation);
xmlWriter.Flush();
stream.Position = 0;
© www.soinside.com 2019 - 2024. All rights reserved.