using HighWayIot.Log4net; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Xml; namespace HighWayIot.Common { /// /// XML读写类 /// public class XmlUtil { private static readonly Lazy lazy = new Lazy(() => new XmlUtil()); public static XmlUtil Instance { get { return lazy.Value; } } /// /// XML读写实例 /// XmlDocument xmlDocument = new XmlDocument(); /// /// 运行时路径 /// private string Path = System.Environment.CurrentDirectory; /// /// 客户端配置读取 /// /// public List ClientReader() { List list = new List(); xmlDocument.Load($"{Path}\\RFIDConfiguration.xml"); XmlNode root = xmlDocument.DocumentElement; XmlNode node = root.SelectSingleNode("Clients"); foreach (XmlNode role in node) { XmlAttribute clientIp = (XmlAttribute)role.Attributes.GetNamedItem("ip"); XmlAttribute clientPort = (XmlAttribute)role.Attributes.GetNamedItem("port"); XmlAttribute clientID = (XmlAttribute)role.Attributes.GetNamedItem("ID"); list.Add(new ClientsConfig() { IP = clientIp.Value, Port = clientPort.Value, ID = clientID.Value }); } return list; } /// /// 读取服务端配置 /// /// public string ServerReader() { xmlDocument.Load($"{Path}\\RFIDConfiguration.xml"); XmlNode root = xmlDocument.DocumentElement; XmlNode node = root.SelectSingleNode("Server"); XmlAttribute ip = (XmlAttribute)node.Attributes.GetNamedItem("ip"); XmlAttribute port = (XmlAttribute)node.Attributes.GetNamedItem("port"); string res = ip.Value + ":" + port.Value; return res; } } public class ClientsConfig { /// /// IP /// public string IP { get; set; } /// /// 端口号 /// public string Port { get; set; } /// /// ID /// public string ID { get; set; } } }