Ejemplo n.º 1
0
        public static XmlDB Load(string filename, string password)
        {
            XmlDB db           = null;
            var   memoryStream = new MemoryStream();

            try
            {
                if (File.Exists(filename))
                {
                    var types    = new List <Type>();
                    var compress = !string.IsNullOrEmpty(password) && CompressUtils.CheckZip(filename, password);
                    if (compress)
                    {
                        using (var fileStream = new FileStream(filename, FileMode.Open))
                        {
                            CompressUtils.Decompress(fileStream, memoryStream, password);
                        }
                        memoryStream.Seek(0, SeekOrigin.Begin);
                    }

                    using (var xmlStream = compress ? (Stream)memoryStream : new FileStream(filename, FileMode.Open))
                    {
                        using (var reader = XmlReader.Create(xmlStream))
                        {
                            while (reader.Read())
                            {
                                if (reader.NodeType == XmlNodeType.Element && reader.LocalName == "DBEntity")
                                {
                                    var typeName = reader.GetAttribute("xsi:type");
                                    types.Add(TypeCache.Instance.GetTypeFromCache(typeof(DBEntity), typeName));
                                }
                            }
                        }

                        types.AddRange(ReflectionUtils.FindTypes(typeof(DBEntity).Assembly, typeof(Geometry)));

                        xmlStream.Seek(0, SeekOrigin.Begin);
                        db = XmlSerializeUtils.Load <XmlDB>(xmlStream, types.Distinct().ToArray()
                                                            /*GetExtraTypes(NAMESPACES)*/, string.Empty);
                        db.DecodeUnprited();
                    }
                }
                else
                {
                    db = new XmlDB {
                        FileName = filename, Tables = new List <XmlTable>()
                    };
                }
            }
            catch (Exception ex)//check it later
            {
                db = new XmlDB {
                    FileName = filename, Tables = new List <XmlTable>()
                };
                LogManager.Instance.Error(ex);
            }
            finally
            {
                db.FileName = filename;
                db.BuildIndex();
                memoryStream.Dispose();
            }
            return(db);
        }