/// <summary> /// 将对象转换成xml文件 /// </summary> public override void ToXML(XmlDocument xmlDoc) { if (!this._orgFilter) { this._viewRange = ViewRangeEnum.NONE; } if (this.DataState == DataState.Add) { XmlNode listNode = xmlDoc.SelectSingleNode("EntityProj/EntityList"); if (listNode == null) { XmlNode root = xmlDoc.SelectSingleNode("EntityProj"); listNode = xmlDoc.CreateElement("EntityList"); root.AppendChild(listNode); } XmlElement el = xmlDoc.CreateElement("Entity"); el.SetAttribute("Guid", this.Guid); el.SetAttribute("Code", this.Code); el.SetAttribute("Name", this.Name); el.SetAttribute("Table", this.TableName); el.SetAttribute("ViewRange", ((int)this.ViewRange).ToString()); el.SetAttribute("OrgFilter", this.OrgFilter ? "1" : "0"); if (!string.IsNullOrEmpty(this.InhertGuid)) { el.SetAttribute("InhertGuid", this.InhertGuid); } if (!string.IsNullOrEmpty(this.FloderGuid)) { el.SetAttribute("FloderGuid", this.FloderGuid); } XmlElement el2 = xmlDoc.CreateElement("Attributes"); el.AppendChild(el2); listNode.AppendChild(el); this.IsChanged = false; foreach (BEColumn col in this.ColumnList) { col.ToXML(xmlDoc); } //从内存中删除已经删除的节点 for (int i = ColumnList.Count - 1; i >= 0; i--) { BEColumn col = ColumnList[i]; if (col.DataState == DataState.Delete) { ColumnList.RemoveAt(i); } } this.DataState = DataState.Update; this.IsChanged = false; } else if (this.DataState == DataState.Update) { if (this.IsChanged) { XmlNode node = xmlDoc.SelectSingleNode("EntityProj/EntityList/Entity[@Guid='" + this.Guid + "']"); node.Attributes["Code"].Value = this.Code; node.Attributes["Name"].Value = this.Name; if (node.Attributes["ViewRange"] == null) { XmlAttribute attr = xmlDoc.CreateAttribute("ViewRange"); attr.Value = ((int)this.ViewRange).ToString(); node.Attributes.Append(attr); } else { node.Attributes["ViewRange"].Value = ((int)this.ViewRange).ToString(); } if (node.Attributes["OrgFilter"] == null) { XmlAttribute attr = xmlDoc.CreateAttribute("OrgFilter"); attr.Value = this.OrgFilter ? "1" : "0"; node.Attributes.Append(attr); } else { node.Attributes["OrgFilter"].Value = this.OrgFilter ? "1" : "0"; } if (string.IsNullOrEmpty(this.FloderGuid)) { if (node.Attributes["FloderGuid"] != null) { node.Attributes.RemoveNamedItem("FloderGuid"); } } else { XmlAttribute attr = xmlDoc.CreateAttribute("FloderGuid"); attr.Value = this.FloderGuid; node.Attributes.Append(attr); } if (node.Attributes["Table"] != null) { node.Attributes["Table"].Value = this.TableName; } else { XmlAttribute attr = xmlDoc.CreateAttribute("Table"); attr.Value = this.TableName; node.Attributes.Append(attr); } if (string.IsNullOrEmpty(this.InhertGuid)) { node.Attributes.RemoveNamedItem("InhertGuid"); } else { if (node.Attributes["InhertGuid"] != null) { node.Attributes["InhertGuid"].Value = this.InhertGuid; } else { XmlAttribute attr = xmlDoc.CreateAttribute("InhertGuid"); attr.Value = this.InhertGuid; node.Attributes.Append(attr); } } this.IsChanged = false; } int columnCount = this.ColumnList.Count; for (int i = 0; i < columnCount; i++) { if (i == this.ColumnList.Count) break; BEColumn col = this.ColumnList[i]; col.ToXML(xmlDoc); } //从内存中删除已经删除的节点 for (int i = ColumnList.Count - 1; i >= 0; i--) { BEColumn col = ColumnList[i]; if (col.DataState == DataState.Delete) { ColumnList.RemoveAt(i); } } } else if (this.DataState == DataState.Delete) { XmlNode node = xmlDoc.SelectSingleNode("EntityProj/EntityList/Entity[@Guid='" + this.Guid + "']"); if (node != null) { node.ParentNode.RemoveChild(node); } } }
/// <summary> /// 从xml文件中初始化对象 /// </summary> /// <param name="xml"></param> public override void FromXML(XmlNode node) { this._guid = node.Attributes["Guid"].Value; this.Code = node.Attributes["Code"].Value; this.Name = node.Attributes["Name"].Value; if (string.IsNullOrEmpty(this.Code)) { this.Code = "BizEntity"; } if (string.IsNullOrEmpty(this.Name)) { this.Name = "未命名实体"; } if (node.Attributes["FloderGuid"] != null) { this.FloderGuid = node.Attributes["FloderGuid"].Value.ToString(); } if (node.Attributes["Table"] == null) { this._tableName = "T_" + this.Code; } else { this._tableName = node.Attributes["Table"].Value.ToString(); } if (node.Attributes["ViewRange"] == null) { this._viewRange = ViewRangeEnum.UPPER; } else { this._viewRange = (ViewRangeEnum)Convert.ToInt32((node.Attributes["ViewRange"].Value)); } if (node.Attributes["OrgFilter"] == null) { this._orgFilter = true; } else { string str = node.Attributes["OrgFilter"].Value; if (str.Trim() == "1") { this._orgFilter = true; } else { this._orgFilter = false; } } if (!this._orgFilter) { this._viewRange = ViewRangeEnum.NONE; } if (node.Attributes["InhertGuid"] != null) { this._inhertGuid = node.Attributes["InhertGuid"].Value; XmlNode entityNode = this.Proj.GetEntityNode(this.InhertGuid); if (entityNode != null) { string inhertNameSpace = entityNode.ParentNode.ParentNode.Attributes["namespace"].Value.ToString(); if (!string.IsNullOrEmpty(inhertNameSpace)) { this._inhertName = inhertNameSpace + "."; } this._inhertName += entityNode.Attributes["Code"].Value.ToString(); } } this.DataState = DataState.Update; //初始化基类列成员 this._inhertColumnList.Clear(); //如果该类是继承下来的话,需要生成基类 if (!string.IsNullOrEmpty(this.InhertGuid)) { List<XmlNode> nodeList = this.GetInhertNodes(this.InhertGuid); foreach (XmlNode inhertNode in nodeList) { BEColumn col = new BEColumn(string.Empty, this.Proj as BEProj, this); col.FromXML(inhertNode); _inhertColumnList.Add(col); } } this._columnList.Clear(); XmlNodeList xmlNodeList = node.SelectNodes("Attributes/Attribute"); //查找当前工程文件下所有的实体 foreach (XmlNode n in xmlNodeList) { BEColumn col = new BEColumn(string.Empty, this.Proj as BEProj, this); col.FromXML(n); this._columnList.Add(col); } this.IsChanged = false; }