using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Xml; using System.Xml.Serialization; using System.IO; namespace BatchService.Framework.Utility { public class XMLHelper { public static T GetEntityFromFile(string xmlFilePath) where T : new() { if (!File.Exists(xmlFilePath)) { SaveEntityToFile(new T(), xmlFilePath); } using (FileStream fs = new FileStream(xmlFilePath, FileMode.Open)) { XmlSerializer xmlSerial = new XmlSerializer(typeof(T)); return (T)xmlSerial.Deserialize(fs); } } public static void SaveEntityToFile(T entity, string xmlFilePath) { if (File.Exists(xmlFilePath)) { File.Delete(xmlFilePath); } XmlSerializer xmlSerial = new XmlSerializer(typeof(T)); using (FileStream fs = new FileStream(xmlFilePath, FileMode.OpenOrCreate, FileAccess.Write)) { xmlSerial.Serialize(fs, entity); } } public static string GetXMLNodeInnerText(XmlDocument xmlDoc, string xPath) { var xmlNode = xmlDoc.SelectSingleNode(xPath); if (xmlNode == null) { return ""; } else { return xmlNode.InnerText; } } } }