/// <summary> /// 保存增量为Xml文件 /// </summary> /// <param name="manager"></param> /// <param name="path">路径</param> /// <returns></returns> public static bool SaveVariable(this XmlDBManager manager, string path) { try { using (var newDb = XmlDB.Create(path)) { var version = MapConfig.Instance.Version; version++; foreach (var table in manager.DB.Tables) { var newTable = XmlTable.Create(table.Name); var entities = table.Entities.Where(o => o.EntityState != EntityState.None); if (entities.Any()) { //更新版本号 entities.ToList().ForEach(o => o.Version = version); newTable.AddRange(entities); newDb.AddTable(newTable); } } InsertCadVersion(version, newDb); newDb.Save(); //保存版本号 SaveVersion(version); return(true); } } catch (Exception ex) { LogManager.Instance.Error(ex); return(false); } }
public void Reset() { if (!string.IsNullOrEmpty(FileName)) { if (_db != null) { _db.Dispose(); } _db = XmlDB.Load(FileName, Password); } else { throw new ArgumentNullException("FileName"); } }
private static void EnumProcess(XmlDB db, TextHandler processHandler) { foreach (var table in db.Tables) { foreach (var entity in table.Entities) { var type = entity.GetType(); foreach (var property in type.GetProperties().Where(o => o.PropertyType == typeof(string))) { var rawText = (string)property.GetValue(entity, null); if (!string.IsNullOrEmpty(rawText)) { property.SetValue(entity, processHandler(rawText), null); } } } } }
/// <summary> /// 创建CAD版本 /// </summary> /// <param name="version"></param> /// <param name="xmlDB"> </param> private static void InsertCadVersion(int version, XmlDB xmlDB) { try { var cadVersion = new CadVersion { G3E_ID = 1, G3E_FID = 1, G3E_FNO = 2014, Version = version, EntityState = EntityState.None }; var versionTable = XmlTable.Create("CadVersion"); versionTable.AddEntity(cadVersion); xmlDB.AddTable(versionTable); } catch { LogManager.Instance.Error("CADVersion添加失败!"); } }
public static XmlDB Create(string filename) { XmlDB db = null; //if (File.Exists(filename)) //{ // try // { // return XmlDB.Load(filename); // } // catch (Exception ex)//modify later // { // } //} //else //{ db = new XmlDB { FileName = filename, Tables = new List <XmlTable>() }; db.BuildIndex(); //} return(db); }
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); }
/// <summary> /// 非打印字符解码 /// </summary> /// <param name="db"></param> public static void DecodeUnprited(this XmlDB db) { EnumProcess(db, text => text.GetLowOrderASCIICharacters()); }