| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- using System;
- using System.Collections;
- using System.IO;
- using System.Xml;
- using IwbZero.ToolCommon.LogHelpers;
- using IwbZero.ToolCommon.StringModel;
- namespace IwbZero.IwbBase
- {
- public class IwbXmlNode
- {
- private int Id { get; }
- private string _value;
- public IwbXmlNode(string pcName, string pcValue, int depth=-1)
- {
- Name = pcName;
- Depth = depth;
- _value = pcValue.FormatXml2Code();
- Id = StringHelper.GetRandom().ValI();
- }
- public void AddChild(IwbXmlNode poNode)
- {
- Nodes.Add(poNode);
- }
- public override bool Equals(object obj)
- {
- return obj != null && (GetHashCode() == obj.GetHashCode());
- }
- public IwbXmlNode GetChildNode(string pcNodeName)
- {
- string pcMessage = "";
- return GetChildNode(pcNodeName, ref pcMessage, false);
- }
- public IwbXmlNode GetChildNode(string pcNodeName, ref string pcMessage, bool plMandatory)
- {
- IwbXmlNode node = null;
- foreach (IwbXmlNode node2 in Nodes)
- {
- if (node2.Name.UAndT() == pcNodeName.UAndT())
- {
- node = node2;
- break;
- }
- }
- if ((node == null) && plMandatory)
- {
- pcMessage = pcMessage + ((pcMessage == "") ? "" : "\r\n") + pcNodeName;
- }
- return node;
- }
- public string GetChildValue(string pcNodeName,bool isFormat=true)
- {
- string str = "";
- IwbXmlNode childNode = GetChildNode(pcNodeName);
- if (childNode != null)
- {
- str = childNode.Value.Trim();
- }
- str = isFormat ? str.FormatXml2Code() : str;
- return str;
- }
- public override int GetHashCode()
- {
- return Id.GetHashCode();
- }
- public override string ToString()
- {
- string name = Name;
- if (Value != "")
- {
- name = name + "=" + Value;
- }
- return name;
- }
- public string ToXmlString(bool isFormat=true)
- {
- if (Nodes.Count == 0)
- {
- string str2 = "<{0}>{1}</{0}>";
- return string.Format(str2, Name, isFormat ? Value.FormatXml2Code() : Value.FormatCode2Xml());
- }
- string format = "<{0}>\r\n{1}\r\n</{0}>";
- string str4 = "";
- foreach (IwbXmlNode node in Nodes)
- {
- str4 = str4 + ((str4 == "") ? "" : "\r\n") + node.ToXmlString(isFormat);
- }
- return string.Format(format, Name, str4);
- }
- public int Depth { get; set; }
- public string Name { get; set; }
- public ArrayList Nodes { get; } = new ArrayList();
- public static IwbXmlNode ParseGenericXml(string pcGenericXml)
- {
- if (pcGenericXml.IsEmpty())
- {
- return null;
- }
- IwbXmlNode node = null;
- StringReader input = new StringReader(pcGenericXml);
- XmlTextReader reader2 = new XmlTextReader(input);
- Hashtable hashtable = new Hashtable();
- try
- {
- while (reader2.Read())
- {
- if (reader2.NodeType == XmlNodeType.Element)
- {
- IwbXmlNode node2 = new IwbXmlNode(reader2.Name, "")
- {
- Depth = reader2.Depth
- };
- if (hashtable[reader2.Depth] == null)
- {
- hashtable.Add(reader2.Depth, node2);
- }
- else
- {
- hashtable[reader2.Depth] = node2;
- }
- if (reader2.Depth > 0)
- {
- ((IwbXmlNode)hashtable[reader2.Depth - 1]).AddChild(node2);
- }
- else
- {
- node = node2;
- }
- }
- if (reader2.NodeType == XmlNodeType.Text)
- {
- ((IwbXmlNode)hashtable[reader2.Depth - 1]).Value = reader2.Value.FormatXml2Code();
- }
- }
- }
- catch (Exception e)
- {
- typeof(IwbXmlNode).LogError(e);
- typeof(IwbXmlNode).LogError($" -------xml:{pcGenericXml}");
- node = null;
- }
- return node;
- }
- public string Value
- {
- get => _value;
- set => _value = value.FormatXml2Code();
- }
- }
- }
|