| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.IO;
- using System.Xml;
- namespace CommonTool
- {
- /// <summary>
- /// jtXmlNode 创建 xml 数据格式
- /// </summary>
- [Serializable]
- public class SnXmlNode : ICloneable
- {
- /// <summary>
- /// 节点构造函数
- /// </summary>
- /// <param name="pcName">节点名称</param>
- /// <param name="pcValue">节点值,是父节点的话,为空</param>
- public SnXmlNode(string pcName, string pcValue)
- {
- //this._Name = "";
- //this._Value = "";
- _depth = -1;
- _nodes = new List<SnXmlNode>();
- _name = pcName;
- _value = Utils.FormatXmlToCode(pcValue);
- _id = Guid.NewGuid().ToString();
- NodeType = XmlNodeType.Element;
- }
- // Fields
- private int _depth;
- private string _id;
- private string _name;
- private readonly List<SnXmlNode> _nodes;
- private string _value;
- object _tag;
- private XmlNodeType _nodeType ;
- public object Tag
- {
- get { return _tag; }
- set { _tag = value; }
- }
- protected XmlNodeType NodeType
- {
- get { return _nodeType; }
- set { _nodeType = 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; }
- }
- /// <summary>
- /// 向xmlNode节点中添加一个子节点
- /// </summary>
- /// <param name="poNode"></param>
- public void AddChild(SnXmlNode poNode)
- {
- _nodes.Add(poNode);
- }
- /// <summary>
- /// 判断Node的子节点中是否包含该节点
- /// </summary>
- /// <param name="poNode"></param>
- /// <returns></returns>
- public bool Contains(SnXmlNode poNode)
- {
- return _nodes.Contains(poNode);
- }
- /// <summary>
- /// 新增一个不同的节点,如果已经包含此节点,则不增加
- /// </summary>
- /// <param name="poNode"></param>
- /// <returns></returns>
- public bool AddNewChild(SnXmlNode poNode)
- {
- bool lbRetVal = false;
- if (!Contains(poNode))
- {
- _nodes.Add(poNode);
- lbRetVal = true;
- }
- return lbRetVal;
- }
- public bool DeleteChild(SnXmlNode poNode)
- {
- bool lbRetVal = false;
- if (_nodes.Contains(poNode))
- {
- lbRetVal = _nodes.Remove(poNode);
- }
- return lbRetVal;
- }
- /// <summary>
- /// 判断两个节点对象是否相等
- /// </summary>
- /// <param name="obj"></param>
- /// <returns></returns>
- public override bool Equals(object obj)
- {
- return obj != null && (GetHashCode() == obj.GetHashCode());
- }
- public bool NameEqual(string pcName)
- {
- return UtilStr.UAndT(_name) == UtilStr.UAndT(pcName);
- }
- /// <summary>
- /// 根据节点名称得到其第一子层的节点
- /// </summary>
- /// <param name="pcNodeName">需要查找的子节点的名称</param>
- /// <returns></returns>
- public SnXmlNode GetChildNode(string pcNodeName)
- {
- string text1 = "";
- return GetChildNode(pcNodeName, ref text1, false);
- }
- /// <summary>
- /// 根据节点名称得到其第一子层的节点
- /// </summary>
- /// <param name="pcNodeName">需要查找的子节点的名称</param>
- /// <param name="pcMessage">没有查找到的节点名称</param>
- /// <param name="plMandatory">是否需要返回没有查找到的节点名称</param>
- /// <returns></returns>
- public SnXmlNode GetChildNode(string pcNodeName, ref string pcMessage, bool plMandatory)
- {
- SnXmlNode node1 = null;
- foreach (SnXmlNode node2 in _nodes)
- {
- if (UtilStr.UAndT(node2.Name) == UtilStr.UAndT(pcNodeName))
- {
- node1 = node2;
- break;
- }
- }
- if ((node1 == null) && plMandatory)
- {
- pcMessage = pcMessage + ((pcMessage == "") ? "" : "\r\n") + pcNodeName;
- }
- return node1;
- }
- /// <summary>
- /// 查找与传入的名称匹配的节点,优先查找同一深度的节点,如果没有再往下一深度的层查找配对的节点
- /// </summary>
- /// <param name="pcDepNodeName">需要匹配的节点名称</param>
- /// <returns></returns>
- public SnXmlNode GetDepChildNode(string pcDepNodeName)
- {
- return GetDepChildNode(Nodes, pcDepNodeName);
- }
- #region ICloneable 成员
- public object Clone()
- {
- SnXmlNode loRetVal = new SnXmlNode(Name, Value);
- foreach (SnXmlNode node in Nodes)
- {
- loRetVal.AddChild((SnXmlNode)node.Clone());
- }
- return loRetVal;
- }
- #endregion
- /// <summary>
- /// 根据节点名称,返回节点的值
- /// </summary>
- /// <param name="pcNodeName"></param>
- /// <returns></returns>
- public string GetChildValue(string pcNodeName)
- {
- string text1 = "";
- SnXmlNode node1 = GetChildNode(pcNodeName);
- if (node1 != null)
- {
- text1 = node1.Value.Trim();
- }
- return text1;
- }
- /// <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)
- {
- SnXmlNode loNode = GetChildNode(pcNodeName);
- if (loNode == null)
- {
- if (pbAddNewIfNull)
- {
- loNode = new SnXmlNode(pcNodeName, pcValue);
- AddChild(loNode);
- }
- }
- else
- {
- loNode.Value = pcValue;
- }
- }
- /// <summary>
- /// 节点的hash代码
- /// </summary>
- /// <returns></returns>
- public override int GetHashCode()
- {
- return Id.GetHashCode();
- }
- /// <summary>
- /// Object对象显示的值
- /// </summary>
- /// <returns></returns>
- public override string ToString()
- {
- string lcRetVal = Name;
- if (Value != "")
- {
- lcRetVal = lcRetVal + "=" + Value;
- }
- return lcRetVal;
- }
- /// <summary>
- /// 将该xmlNode 导成 xml字符串形式,不包括xml声明和所有的注释信息
- /// </summary>
- /// <returns></returns>
- public string ToXmlString()
- {
- return ToXmlString(0);
- }
- private string ToXmlString(int piDepth)
- {
- if (Nodes.Count == 0)
- {
- string lcText = "{2}<{0}>{1}</{0}>";
- return string.Format(lcText, Name, Utils.FormatCodeToXml(Value), UtilStr.Replicate(" ", piDepth));
- }
- string text3 = "{2}<{0}>\r\n{1}\r\n{2}</{0}>";
- string text4 = "";
- foreach (SnXmlNode node1 in Nodes)
- {
- text4 += ((text4 == "") ? "" : "\r\n") + node1.ToXmlString(piDepth + 1);
- }
- return string.Format(text3, Name, text4, UtilStr.Replicate(" ", piDepth));
- }
- /// <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 (Nodes.Count == 0)
- {
- string lcText = "{2}<{0}>{1}</{0}>";
- lcRetVal = string.Format(lcText, Name, Utils.FormatCodeToXml(Value), UtilStr.Replicate(" ", piDepth));
- }
- else
- {
- string text3 = "{2}<{0}>\r\n{1}\r\n{2}</{0}>";
- string text4 = "";
- foreach (SnXmlNode node1 in Nodes)
- {
- text4 += ((text4 == "") ? "" : "\r\n") + node1.ToAllXmlString(piDepth + 1);
- }
- lcRetVal = string.Format(text3, Name, text4, UtilStr.Replicate(" ", piDepth));
- }
- if (_comment.Trim().Length > 0)
- {
- lcRetVal = UtilStr.Replicate(" ", piDepth) + _comment + "\r\n" + lcRetVal;
- }
- return lcRetVal;
- }
- /// <summary>
- /// 节点深度
- /// </summary>
- public int Depth
- {
- get { return _depth; }
- set { _depth = value; }
- }
- /// <summary>
- /// 节点名称
- /// </summary>
- public string Name
- {
- get { return _name; }
- set { _name = value; }
- }
- /// <summary>
- /// 子节点集合
- /// </summary>
- public List<SnXmlNode> Nodes => _nodes;
- /// <summary>
- /// 节点值
- /// </summary>
- public string Value
- {
- get { return _value; }
- set { _value = value; }
- }
- public string Id
- {
- get { return _id; }
- set { _id = value; }
- }
- /// <summary>
- /// 从标准的xml文件转换成 snXmlNode 对象
- /// </summary>
- /// <param name="pcGenericXml"></param>
- /// <returns></returns>
- public static SnXmlNode ParseGenericXml(string pcGenericXml)
- {
- SnXmlNode loRetVal = null;
- //XmlDocument loDocument = new XmlDocument();
- //loDocument.LoadXml(pcGenericXml);
- StringReader loStrReader = new StringReader(pcGenericXml);
- XmlTextReader loXmlReader = new XmlTextReader(loStrReader);
- Hashtable hashtable1 = new Hashtable();
- string lcContent = "";
- string lcXmlDeclaration = "";
- try
- {
- while (loXmlReader.Read())
- {
- if (loXmlReader.NodeType == XmlNodeType.Element)
- {
- SnXmlNode node2 = new SnXmlNode(loXmlReader.Name, "");
- if (lcContent.Trim().Length > 0)
- {
- node2._comment = lcContent;
- lcContent = "";
- }
- node2.Depth = loXmlReader.Depth;
- if (hashtable1[loXmlReader.Depth] == null)
- {
- hashtable1.Add(loXmlReader.Depth, node2);
- }
- else
- {
- hashtable1[loXmlReader.Depth] = node2;
- }
- if (loXmlReader.Depth > 0)
- {
- ((SnXmlNode)hashtable1[loXmlReader.Depth - 1]).AddChild(node2);
- }
- else
- {
- loRetVal = node2;
- }
- }
- if (loXmlReader.NodeType == XmlNodeType.XmlDeclaration)
- {
- lcXmlDeclaration = "<?" + loXmlReader.Name + " " + loXmlReader.Value + "?>";
- }
- if (loXmlReader.NodeType == XmlNodeType.Comment)
- {
- lcContent = "<!--" + loXmlReader.Value + "-->";
- }
- if (loXmlReader.NodeType == XmlNodeType.Text)
- {
- ((SnXmlNode)hashtable1[loXmlReader.Depth - 1]).Value = Utils.FormatXmlToCode(loXmlReader.Value);
- }
- }
- if (loRetVal != null)
- {
- loRetVal._xmlDeclaration = lcXmlDeclaration;
- }
- }
- catch (Exception e)
- {
- throw new Exception(e.Message, e);
- }
- return loRetVal;
- }
- /// <summary>
- /// 查找与传入的名称匹配的节点,优先查找同一深度的节点,如果没有再往下一深度的层查找配对的节点
- /// </summary>
- /// <param name="poListNode">需要查找的节点层</param>
- /// <param name="pcDepNodeName">匹配的节点名称</param>
- /// <returns>返回匹配的同一个层中第一个节点</returns>
- public static SnXmlNode GetDepChildNode(List<SnXmlNode> poListNode, string pcDepNodeName)
- {
- SnXmlNode loRetVal = null;
- if (poListNode != null && poListNode.Count > 0)
- {
- List<SnXmlNode> loList = new List<SnXmlNode>();
- foreach (SnXmlNode loNode in poListNode)
- {
- if (loNode != null)
- {
- if (UtilStr.UAndT(loNode.Name) == UtilStr.UAndT(pcDepNodeName))
- {
- loRetVal = loNode;
- break;
- }
- foreach (SnXmlNode node in loNode.Nodes)
- {
- loList.Add(node);
- }
- }
- }
- if (loRetVal == null && loList.Count > 0)
- {
- loRetVal = GetDepChildNode(loList, pcDepNodeName);
- }
- }
- return loRetVal;
- }
- }
- }
|