| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Xml.Linq;
- using DBConn;
- namespace Core.ONH.Collection.comm
- {
- public class XmlHelper
- {
- private static readonly object LockHelper = new object();
- public static void GenerateXmlFile(string xmlPath)
- {
- try
- {
- //定义一个XDocument结构
- XDocument myXDoc = new XDocument(
- new XElement("Root"
- )
- );
- myXDoc.Save(xmlPath);
- }
- catch (Exception ex)
- {
- Console.WriteLine(ex.ToString());
- }
- }
- public static string GetXmlData(string path, string attri, string defaultVaule = "")
- {
- lock (LockHelper)
- {
- if (!File.Exists(path))
- GenerateXmlFile(path);
- XElement rootNode = XElement.Load(path);
- var attribute = rootNode.Attributes().FirstOrDefault(p => p.Name == attri);
- return attribute != null ? attribute.Value : defaultVaule;
- }
- }
- public static double GetXmlData(string path, string attri, double defaultVaule = 0d)
- {
- lock (LockHelper)
- {
- if (!File.Exists(path))
- GenerateXmlFile(path);
- XElement rootNode = XElement.Load(path);
- var attribute = rootNode.Attributes().FirstOrDefault(p => p.Name == attri);
- double returnValue;
- return attribute == null || !double.TryParse(attribute.Value, out returnValue) ? defaultVaule : returnValue;
- }
- }
- public static int GetXmlData(string path, string attri, int defaultVaule = 0)
- {
- lock (LockHelper)
- {
- if (!File.Exists(path))
- GenerateXmlFile(path);
-
- XElement rootNode = XElement.Load(path);
- var attribute = rootNode.Attributes().FirstOrDefault(p => p.Name == attri);
-
- int returnValue;
- return attribute == null || !int.TryParse(attribute.Value, out returnValue) ? defaultVaule : returnValue;
- }
-
- }
- public static bool GetXmlData(string path, string attri, bool defaultVaule = false)
- {
- lock (LockHelper)
- {
- if (!File.Exists(path))
- GenerateXmlFile(path);
- XElement rootNode = XElement.Load(path);
- var attribute = rootNode.Attributes().FirstOrDefault(p => p.Name == attri);
- int returnValue;
- return attribute == null || !int.TryParse(attribute.Value, out returnValue) ? defaultVaule : returnValue==1;
- }
- }
- public static void SetXmlData(string path, string attri, object data)
- {
- lock (LockHelper)
- {
- if (!File.Exists(path))
- GenerateXmlFile(path);
- XElement rootNode = XElement.Load(path);
- rootNode.SetAttributeValue(attri, data);
- rootNode.Save(path);
- }
-
- }
- public static void AddNodes(string path, string xe, object data,string keyProperties,string KeyValue, List<string> WriteProperties = null )
- {
- lock (LockHelper)
- {
- if (!File.Exists(path))
- GenerateXmlFile(path);
- XElement rootNode = XElement.Load(path);
- XElement NewElement = new XElement(xe);
- foreach (var propertyInfo in data.GetType().GetProperties().AsParallel())
- {
- if (WriteProperties != null)
- {
- if (!WriteProperties.Contains(propertyInfo.Name))
- {
- continue;
- }
- NewElement.Add(new XAttribute(propertyInfo.Name, propertyInfo.GetValue(data, null).ToString()));
- }
- }
- if (keyProperties != "")
- {
- rootNode.Elements(xe).Where(p =>
- {
- var xAttribute = p.Attribute(keyProperties);
- return xAttribute != null && xAttribute.Value == KeyValue;
- }).ToList().ForEach(p => p.Remove());
- }
- rootNode.Add(NewElement);
- rootNode.Save(path);
-
- }
- }
- public static void ChangeNodes(string path, string xe, object data, string keyProperties, string KeyValue,
- List<string> WriteProperties = null)
- {
- lock (LockHelper)
- {
- if (!File.Exists(path))
- GenerateXmlFile(path);
- XElement rootNode = XElement.Load(path);
- var xElements = rootNode.Elements(xe).Where(p =>
- {
- var xAttribute = p.Attribute(keyProperties);
- return xAttribute != null && xAttribute.Value == KeyValue;
- }).ToList();
- if (xElements.Count <= 0)
- {
- AddNodes(path, xe, data, keyProperties, KeyValue, WriteProperties);
- return;
- }
- XElement NewElement = xElements[0];
- foreach (var propertyInfo in data.GetType().GetProperties().AsParallel())
- {
- if (WriteProperties != null)
- {
- if (!WriteProperties.Contains(propertyInfo.Name))
- {
- continue;
- }
- var atrr = NewElement.Attribute(propertyInfo.Name);
- if (atrr == null)
- {
- NewElement.Add(new XAttribute(propertyInfo.Name,
- propertyInfo.GetValue(data, null).ToString()));
- }
- else
- {
- atrr.SetValue(propertyInfo.GetValue(data, null).ToString());
- }
- }
- }
- rootNode.Save(path);
- }
- }
- public static void DeleteNodes(string path, string xe, string keyProperties, string KeyValue)
- {
- lock (LockHelper)
- {
- if (!File.Exists(path))
- GenerateXmlFile(path);
- XElement rootNode = XElement.Load(path);
- if (keyProperties != "")
- {
- rootNode.Elements(xe).Where(p =>
- {
- var xAttribute = p.Attribute(keyProperties);
- return xAttribute != null && xAttribute.Value == KeyValue;
- }).ToList().ForEach(p => p.Remove());
- }
- rootNode.Save(path);
- }
- }
- public static List<Dictionary<string, string>> GetXmlAttributes(string path, string Node)
- {
- lock (LockHelper)
- {
- if (!File.Exists(path))
- GenerateXmlFile(path);
- XElement rootNode = XElement.Load(path);
- List<Dictionary<string,string>> datas = new List<Dictionary<string, string>>();
- foreach (var Element in rootNode.Elements(Node))
- {
- Dictionary<string,string> data = new Dictionary<string, string>();
- foreach (var Attribute in Element.Attributes())
- {
- data.Add(Attribute.Name.ToString(),Attribute.Value);
- }
- datas.Add(data);
- }
- return datas;
- }
- }
- public static List<T> GetXmlAttributes<T>(string path, string Node) where T : new()
- {
- lock (LockHelper)
- {
- if (!File.Exists(path))
- GenerateXmlFile(path);
- XElement rootNode = XElement.Load(path);
- return new ModelDbHandler<T>().GetModelByElements(rootNode.Elements(Node).ToList());
- }
- }
- }
- }
|