123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304 |
- using System;
- using System.Collections;
- using System.Collections.Generic ;
- using System.Xml;
- using System.Text;
- using System.IO;
- namespace SysBaseLibs
- {
- /// <summary>
- /// rsXmlNode 创建 xml 数据格式
- /// songyf 200606 v1.0.0
- /// </summary>
- public class rsXmlNode
- {
- public rsXmlNode(string pcName, string pcValue)
- {
- this._Name = "";
- this._Value = "";
- this._Depth = -1;
- this._Id = -1;
- this._Nodes = new List<rsXmlNode >();
- 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<rsXmlNode> _Nodes;
- private string _Value;
- private string _XmlDeclaration = "";
- /// <summary>
- /// xml 声明信息
- /// </summary>
- public string XmlDeclaration
- {
- get { return _XmlDeclaration; }
- set { _XmlDeclaration = value; }
- }
- private string _Comment = "";
- /// <summary>
- /// 该节点注释信息
- /// </summary>
- 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}</{0}>";
- // return string.Format(text2, this.Name, this.Value);
- // }
- // string text3 = "<{0}>\r\n{1}\r\n</{0}>";
- // 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}</{0}>";
- return string.Format(text2, this.Name, FormatCodeToXML(this.Value));
- }
- string text3 = "<{0}>\r\n{1}\r\n</{0}>";
- 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<rsXmlNode> 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;
- }
- /// <summary>
- /// 给节点重新赋值,如果没有发现该节点则创建该节点
- /// </summary>
- /// <param name="pcNodeName"></param>
- /// <param name="pcValue"></param>
- public void SetChildValue(string pcNodeName, string pcValue)
- {
- SetChildValue(pcNodeName, pcValue, true);
- }
- /// <summary>
- /// 给节点重新赋值
- /// </summary>
- /// <param name="pcNodeName">节点名称</param>
- /// <param name="pcValue">节点值</param>
- /// <param name="pbAddNewIfNull">当节点为null的时候,是否创建该节点</param>
- 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;
- }
- }
- /// <summary>
- /// 将该xmlNode 导成 xml字符串形式,包括xml声明和所有的注释信息
- /// </summary>
- /// <returns></returns>
- 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}</{0}>";
- lcRetVal = string.Format(lcText, this.Name, Utils.FormatCodeToXML(this.Value), UtilStr.Replicate(" ", piDepth));
- }
- else
- {
- string text3 = "{2}<{0}>\r\n{1}\r\n{2}</{0}>";
- 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;
- }
- }
- }
|