예제 #1
0
        public static bool SerializeToXml(ICanSerialize obj, XmlWriter xw, Predicate <ICanSerialize> canSerialize, object serializeParam, List <ICanSerialize> serializedObjects, bool useTextFragments = false)
        {
            if (canSerialize(obj))
            {
                if (serializedObjects != null)
                {
                    serializedObjects.Add(obj);
                }

                SerializeData xd = obj.GetSerializeData(serializeParam);

                if (xd != null)
                {
                    if (xd.ElementName != null)
                    {
                        xw.WriteStartElement(xd.ElementName);

                        if (xd.Attributes != null)
                        {
                            foreach (var attribute in xd.Attributes)
                            {
                                xw.WriteAttributeString(attribute.Key, attribute.Value);
                            }
                        }
                        if (useTextFragments == false)
                        {
                            xw.WriteString(xd.Content);
                        }
                    }
                    else if (useTextFragments)
                    {
                        xw.WriteString(xd.Content);
                        return(true);
                    }
                    if (xd.Components != null)
                    {
                        foreach (var child in xd.Components)
                        {
                            SerializeToXml(child, xw, canSerialize, serializeParam, serializedObjects, useTextFragments);
                        }
                    }
                    if (xd.ElementName != null)
                    {
                        xw.WriteEndElement();
                    }

                    return(true);
                }
                return(false);
            }
            return(false);
        }
예제 #2
0
        /// <summary>
        /// 保存对象
        /// </summary>
        /// <param name="path">路径</param>
        /// <param name="obj">对象</param>
        /// <param name="extraTypes">额外的类型,若对象中存在自定义类型的对象,则必须将这些对象输入</param>
        public static void Save(string path, ICanSerialize obj, Type[] extraTypes, string password)
        {
            //using (FileStream outputStream = new FileStream(path,FileMode.Create))
            //{
            //    using (MemoryStream memoryStream = new MemoryStream())
            //    {
            //        XmlSerializer serializer = XmlSerializerFactory.Instance.Create(obj.GetType(), extraTypes);//new XmlSerializer(obj.GetType(), extraTypes);
            //        serializer.Serialize(memoryStream, obj);

            //        memoryStream.Seek(0, SeekOrigin.Begin);
            //        if (!string.IsNullOrEmpty(password))
            //        {

            //            CompressUtils.Compress(memoryStream, outputStream, password);
            //        }
            //        else
            //        {
            //            memoryStream.WriteTo(outputStream);
            //        }


            //    }


            //}
            var tempFilePath = Path.GetTempFileName();

            using (var tempFileWriter = new StreamWriter(tempFilePath, false, Encoding.UTF8))
            {
                XmlSerializer serializer = XmlSerializerFactory.Instance.Create(obj.GetType(), extraTypes);
                serializer.Serialize(tempFileWriter, obj);
            }
            if (File.Exists(path))
            {
                File.Delete(path);
            }
            if (!string.IsNullOrEmpty(password))
            {
                CompressUtils.Compress(tempFilePath, path, password);
                File.Delete(tempFilePath);
            }
            else
            {
                File.Move(tempFilePath, path);
            }
        }
예제 #3
0
 public void Inject(PacketType t, ICanSerialize p, int sockId)
 {
     //serialize the packet
     byte[] dataPacket = p.Serialize();
     Inject(t, dataPacket, sockId);
 }
예제 #4
0
 public static byte[] Encode(PacketType type, ICanSerialize p)
 {
     //serialize the packet
     byte[] data = p.Serialize();
     return(Encode(type, data));
 }
예제 #5
0
 public static void Save(string path, ICanSerialize obj, Type[] extraTypes)
 {
     Save(path, obj, extraTypes, string.Empty);
 }
예제 #6
0
        /// <summary>
        /// 另存为
        /// </summary>
        /// <param name="obj">的预想</param>
        /// <param name="path">路径</param>
        /// <param name="extraTypes">额外的类型,若对象中存在自定义类型的对象,则必须将这些对象输入</param>
        /// <param name="inverse">反码保存</param>
        public static void SaveAs(this ICanSerialize obj, string path, Type[] extraTypes, string password)

        {
            Save(path, obj, extraTypes, password);
        }