using System; using System.Collections; using System.Collections.Generic ; using System.Xml; using System.Text; using System.IO; namespace SysBaseLibs { /// /// rsXmlNode 创建 xml 数据格式 /// songyf 200606 v1.0.0 /// public class rsXmlNode { public rsXmlNode(string pcName, string pcValue) { this._Name = ""; this._Value = ""; this._Depth = -1; this._Id = -1; this._Nodes = new List(); this._Name = pcName; this._Value = Utils.FormatXMLToCode(pcValue); this._Id = Utils.ValI(AppEnv.GetRandom()); } // Fields private int _Depth; private int _Id; private string _Name; private List _Nodes; private string _Value; private string _XmlDeclaration = ""; /// /// xml 声明信息 /// public string XmlDeclaration { get { return _XmlDeclaration; } set { _XmlDeclaration = value; } } private string _Comment = ""; /// /// 该节点注释信息 /// public string Comment { get { return _Comment; } set { _Comment = value; } } // Methods public void AddChild(rsXmlNode poNode) { this._Nodes.Add(poNode); } public override bool Equals(object obj) { return (this.GetHashCode() == obj.GetHashCode()); } public rsXmlNode GetChildNode(string pcNodeName) { string text1 = ""; return this.GetChildNode(pcNodeName, ref text1, false); } public rsXmlNode GetChildNode(string pcNodeName, ref string pcMessage, bool plMandatory) { rsXmlNode node1 = null; foreach (rsXmlNode node2 in this._Nodes) { if (UtilStr.UAndT(node2.Name) == UtilStr.UAndT(pcNodeName)) { node1 = node2; break; } } if ((node1 == null) && plMandatory) { pcMessage = pcMessage + ((pcMessage == "") ? "" : "\r\n") +pcNodeName;// AppEnv.T(0x18767, pcNodeName); } return node1; } public string GetChildValue(string pcNodeName) { string text1 = ""; rsXmlNode node1 = this.GetChildNode(pcNodeName); if (node1 != null) { text1 = node1.Value.Trim(); } return text1; } public override int GetHashCode() { return this._Id.GetHashCode(); } public override string ToString() { string text1 = this.Name; if (this.Value != "") { text1 = text1 + "=" + this.Value; } return text1; } //public string ToXmlStringNoTrans() //{ // if (this.Nodes.Count == 0) // { // string text2 = "<{0}>{1}"; // return string.Format(text2, this.Name, this.Value); // } // string text3 = "<{0}>\r\n{1}\r\n"; // string text4 = ""; // foreach (rsXmlNode node1 in this.Nodes) // { // text4 = text4 + ((text4 == "") ? "" : "\r\n") + node1.ToXmlStringNoTrans(); // } // return string.Format(text3, this.Name, text4); //} public string ToXmlString() { if (this.Nodes.Count == 0) { string text2 = "<{0}>{1}"; return string.Format(text2, this.Name, FormatCodeToXML(this.Value)); } string text3 = "<{0}>\r\n{1}\r\n"; string text4 = ""; foreach (rsXmlNode node1 in this.Nodes) { text4 = text4 + ((text4 == "") ? "" : "\r\n") + node1.ToXmlString(); } return string.Format(text3, this.Name, text4); } // Properties public int Depth { get{return _Depth;} set{ _Depth=value;} } public string Name { get{return _Name;} set{_Name=value;} } public List Nodes { get {return _Nodes;} } public string Value { get{return _Value;} set{_Value=value;} } public static string FormatCodeToXML(string pcString) { return pcString.Replace("<", ";lt;").Replace(">", ";gt;").Replace("&", ";amp;"); } public static string FormatXMLToCode(string pcString) { return pcString.Replace(";amp;", "&").Replace(";lt;", "<").Replace(";gt;", ">"); } #region ICloneable 成员 public object Clone() { rsXmlNode loRetVal = new rsXmlNode(this.Name, this.Value); foreach (rsXmlNode Node in this.Nodes) { loRetVal.AddChild((rsXmlNode)Node.Clone()); } return loRetVal; } #endregion public static rsXmlNode ParseGenericXml(string pcGenericXml) { rsXmlNode node1 = null; StringReader reader1 = new StringReader(pcGenericXml); XmlTextReader reader2 = new XmlTextReader(reader1); Hashtable hashtable1 = new Hashtable(); try { while (reader2.Read()) { if (reader2.NodeType == XmlNodeType.Element) { rsXmlNode node2 = new rsXmlNode(reader2.Name, ""); node2.Depth = reader2.Depth; if (hashtable1[reader2.Depth] == null) { hashtable1.Add(reader2.Depth, node2); } else { hashtable1[reader2.Depth] = node2; } if (reader2.Depth > 0) { ((rsXmlNode)hashtable1[reader2.Depth - 1]).AddChild(node2); } else { node1 = node2; } } if (reader2.NodeType == XmlNodeType.Text) { ((rsXmlNode)hashtable1[reader2.Depth - 1]).Value = FormatXMLToCode(reader2.Value); } } } catch (Exception e) { throw new Exception(e.Message, e); } return node1; } /// /// 给节点重新赋值,如果没有发现该节点则创建该节点 /// /// /// public void SetChildValue(string pcNodeName, string pcValue) { SetChildValue(pcNodeName, pcValue, true); } /// /// 给节点重新赋值 /// /// 节点名称 /// 节点值 /// 当节点为null的时候,是否创建该节点 public void SetChildValue(string pcNodeName, string pcValue, bool pbAddNewIfNull) { rsXmlNode loNode = this.GetChildNode(pcNodeName); if (loNode == null) { if (pbAddNewIfNull) { loNode = new rsXmlNode(pcNodeName, pcValue); this.AddChild(loNode); } } else { loNode.Value = pcValue; } } /// /// 将该xmlNode 导成 xml字符串形式,包括xml声明和所有的注释信息 /// /// public string ToAllXmlString() { string lcRetVal = ToAllXmlString(0); if (_XmlDeclaration.Trim().Length > 0) lcRetVal = _XmlDeclaration + "\r\n" + lcRetVal; return lcRetVal; } private string ToAllXmlString(int piDepth) { string lcRetVal = ""; if (this.Nodes.Count == 0) { string lcText = "{2}<{0}>{1}"; lcRetVal = string.Format(lcText, this.Name, Utils.FormatCodeToXML(this.Value), UtilStr.Replicate(" ", piDepth)); } else { string text3 = "{2}<{0}>\r\n{1}\r\n{2}"; string text4 = ""; foreach (rsXmlNode node1 in this.Nodes) { text4 += ((text4 == "") ? "" : "\r\n") + node1.ToAllXmlString(piDepth + 1); } lcRetVal = string.Format(text3, this.Name, text4, UtilStr.Replicate(" ", piDepth)); } if (_Comment.Trim().Length > 0) { lcRetVal = UtilStr.Replicate(" ", piDepth) + _Comment + "\r\n" + lcRetVal; } return lcRetVal; } } }