| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Xml.Linq;
- using System.Xml.Serialization;
- namespace Core.StlMes.Client.Mcp.Mch.Mcms
- {
- class XmlHelper
- {
- private static readonly object LockHelper = new object();
- private static void GenerateXmlFile(string xmlPath)
- {
- try
- {
- //定义一个XDocument结构
- var 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);
- var 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);
- var 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);
- var 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 void SetXmlData(string path, string attri, object data)
- {
- lock (LockHelper)
- {
- if (!File.Exists(path))
- GenerateXmlFile(path);
- var rootNode = XElement.Load(path);
- rootNode.SetAttributeValue(attri, data);
- rootNode.Save(path);
- }
- }
- public static void SetXmlEntityData<T>(string path, T data)
- {
- lock (LockHelper)
- {
- try
- {
- var myXDoc = new XDocument();
- myXDoc.Add(ToXElement(data));
- myXDoc.Save(path);
- }
- catch (Exception ex)
- {
- Console.WriteLine(ex.ToString());
- }
- }
- }
- public static void SetXmlEntityData<T>(string path,string node, T data)
- {
- lock (LockHelper)
- {
- try
- {
- var rootNode = XElement.Load(path);
- rootNode.Add(ToXElement(data));
- rootNode.Save(path);
- }
- catch (Exception ex)
- {
- Console.WriteLine(ex.ToString());
- }
- }
- }
- public static T FromXElement<T>(XElement xElement)
- {
- var xmlSerializer = new XmlSerializer(typeof(T));
- return (T)xmlSerializer.Deserialize(xElement.CreateReader());
- }
- public static T GetXmlData<T>(string path) where T : new()
- {
- lock (LockHelper)
- {
- if (!File.Exists(path))
- {
- return new T();
- }
- var rootNode = XElement.Load(path);
- return FromXElement<T>(rootNode);
- }
- }
- private static T SetEntity<T>(List<XElement> list) where T : new()
- {
- T entity = new T();
- foreach (var propertyInfo in typeof(T).GetProperties())
- {
- var info = propertyInfo;
- var data = list.FirstOrDefault(p => p.Name == info.Name);
- if (data != null)
- {
- if (propertyInfo.PropertyType.FullName != null && propertyInfo.PropertyType.FullName.StartsWith("System."))
- {
- propertyInfo.SetValue(entity, data.Value, null);
- }
- else
- {
- /* Type rType = propertyInfo.PropertyType;
- propertyInfo.SetValue(entity, SetEntity<rType>()
- , null);*/
- }
- }
- }
- return entity;
- }
- public static XElement ToXElement<T>(T obj)
- {
- using (var memoryStream = new MemoryStream())
- {
- using (TextWriter streamWriter = new StreamWriter(memoryStream))
- {
- var xmlSerializer = new XmlSerializer(typeof(T));
- xmlSerializer.Serialize(streamWriter, obj);
- return XElement.Parse(Encoding.UTF8.GetString(memoryStream.ToArray()));
- }
- }
- }
- }
- }
|