using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Xml;
namespace CommonTool
{
///
/// jtXmlNode 创建 xml 数据格式
///
[Serializable]
public class SnXmlNode : ICloneable
{
///
/// 节点构造函数
///
/// 节点名称
/// 节点值,是父节点的话,为空
public SnXmlNode(string pcName, string pcValue)
{
//this._Name = "";
//this._Value = "";
_depth = -1;
_nodes = new List();
_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 _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 = "";
///
/// xml 声明信息
///
public string XmlDeclaration
{
get { return _xmlDeclaration; }
set { _xmlDeclaration = value; }
}
private string _comment = "";
///
/// 该节点注释信息
///
public string Comment
{
get { return _comment; }
set { _comment = value; }
}
///
/// 向xmlNode节点中添加一个子节点
///
///
public void AddChild(SnXmlNode poNode)
{
_nodes.Add(poNode);
}
///
/// 判断Node的子节点中是否包含该节点
///
///
///
public bool Contains(SnXmlNode poNode)
{
return _nodes.Contains(poNode);
}
///
/// 新增一个不同的节点,如果已经包含此节点,则不增加
///
///
///
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;
}
///
/// 判断两个节点对象是否相等
///
///
///
public override bool Equals(object obj)
{
return obj != null && (GetHashCode() == obj.GetHashCode());
}
public bool NameEqual(string pcName)
{
return UtilStr.UAndT(_name) == UtilStr.UAndT(pcName);
}
///
/// 根据节点名称得到其第一子层的节点
///
/// 需要查找的子节点的名称
///
public SnXmlNode GetChildNode(string pcNodeName)
{
string text1 = "";
return GetChildNode(pcNodeName, ref text1, false);
}
///
/// 根据节点名称得到其第一子层的节点
///
/// 需要查找的子节点的名称
/// 没有查找到的节点名称
/// 是否需要返回没有查找到的节点名称
///
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;
}
///
/// 查找与传入的名称匹配的节点,优先查找同一深度的节点,如果没有再往下一深度的层查找配对的节点
///
/// 需要匹配的节点名称
///
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
///
/// 根据节点名称,返回节点的值
///
///
///
public string GetChildValue(string pcNodeName)
{
string text1 = "";
SnXmlNode node1 = GetChildNode(pcNodeName);
if (node1 != null)
{
text1 = node1.Value.Trim();
}
return text1;
}
///
/// 给节点重新赋值,如果没有发现该节点则创建该节点
///
///
///
public void SetChildValue(string pcNodeName, string pcValue)
{
SetChildValue(pcNodeName, pcValue, true);
}
///
/// 给节点重新赋值
///
/// 节点名称
/// 节点值
/// 当节点为null的时候,是否创建该节点
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;
}
}
///
/// 节点的hash代码
///
///
public override int GetHashCode()
{
return Id.GetHashCode();
}
///
/// Object对象显示的值
///
///
public override string ToString()
{
string lcRetVal = Name;
if (Value != "")
{
lcRetVal = lcRetVal + "=" + Value;
}
return lcRetVal;
}
///
/// 将该xmlNode 导成 xml字符串形式,不包括xml声明和所有的注释信息
///
///
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));
}
///
/// 将该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 (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;
}
///
/// 节点深度
///
public int Depth
{
get { return _depth; }
set { _depth = value; }
}
///
/// 节点名称
///
public string Name
{
get { return _name; }
set { _name = value; }
}
///
/// 子节点集合
///
public List Nodes => _nodes;
///
/// 节点值
///
public string Value
{
get { return _value; }
set { _value = value; }
}
public string Id
{
get { return _id; }
set { _id = value; }
}
///
/// 从标准的xml文件转换成 snXmlNode 对象
///
///
///
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 = "";
}
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;
}
///
/// 查找与传入的名称匹配的节点,优先查找同一深度的节点,如果没有再往下一深度的层查找配对的节点
///
/// 需要查找的节点层
/// 匹配的节点名称
/// 返回匹配的同一个层中第一个节点
public static SnXmlNode GetDepChildNode(List poListNode, string pcDepNodeName)
{
SnXmlNode loRetVal = null;
if (poListNode != null && poListNode.Count > 0)
{
List loList = new List();
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;
}
}
}