XML新增、修改、选择
using System; using System.Linq; using System.Xml.Linq; using DFS.Kiosk.Provider.Simulator.Common.Utilities; using DFS.Kiosk.Provider.Simulators.Utilities; namespace DFS.Kiosk.Provider.Simulator.Common.Data { public static class DataAccessor { private static readonly object ReadWriteLock = new object(); private static XDocument _doc; private const string XmlFileName = "Data/ValidTickets.xml"; private static XDocument Doc { get { return _doc ?? (_doc = XDocument.Load(XmlFileName)); } } public static string ReadTicket(string ticketNumber, TicketStatus ticketstatus) { try { lock (ReadWriteLock) { XElement element = (from elem in Doc.Elements().Descendants() where elem.Value == ticketNumber && elem.Attribute("status").Value == ticketstatus.ToString() select elem).FirstOrDefault(); return element != null ? element.Attribute("amount").Value : "0"; } } catch (Exception ex) { Logger.WriteLogger(ex, ConstValues.MainLogFileName); } return "0"; } public static string ReadKonamiTicket(string ticketNumber, TicketStatus ticketstatus) { try { lock (ReadWriteLock) { XElement element = (from elem in Doc.Elements().Descendants() where elem.Value.Substring(2,16) == ticketNumber && elem.Attribute("status").Value == ticketstatus.ToString() select elem).FirstOrDefault(); return element != null ? element.Attribute("amount").Value : "0"; } } catch (Exception ex) { Logger.WriteLogger(ex, ConstValues.MainLogFileName); } return "0"; } public static string ReadTicket(string ticketNumber, string attribute) { try { lock (ReadWriteLock) { XElement element = (from elem in Doc.Elements().Descendants() where elem.Value == ticketNumber select elem) .FirstOrDefault(); if (attribute == "status") { return element != null ? element.Attribute("status").Value : TicketStatus.NotFound.ToString(); } if (attribute == "amount") { return element != null ? element.Attribute("amount").Value : "0"; } } } catch (Exception ex) { Logger.WriteLogger(ex, ConstValues.MainLogFileName); } return "0"; } public static bool WriteTicket(string ticketNumber, TicketStatus oldStatus, TicketStatus newStatus) { try { lock (ReadWriteLock) { XElement element = (from elem in Doc.Elements().Descendants() where elem.Value == ticketNumber && elem.Attribute("status").Value == oldStatus.ToString() select elem).FirstOrDefault(); if (element != null) { element.Attribute("status").Value = newStatus.ToString(); return true; } return false; } } catch (Exception ex) { Logger.WriteLogger(ex, ConstValues.MainLogFileName); return false; } } public static bool WriteKonamiTicket(string ticketNumber, TicketStatus oldStatus, TicketStatus newStatus) { try { lock (ReadWriteLock) { XElement element = (from elem in Doc.Elements().Descendants() where elem.Value.Substring(2,16) == ticketNumber && elem.Attribute("status").Value == oldStatus.ToString() select elem).FirstOrDefault(); if (element != null) { element.Attribute("status").Value = newStatus.ToString(); return true; } return false; } } catch (Exception ex) { Logger.WriteLogger(ex, ConstValues.MainLogFileName); return false; } } /// <summary> /// Creates ticket, add xml node into ValidTickets.xml file /// </summary> /// <param name="amount">The amount.</param> /// <returns></returns> public static string CreateTicket(string amount) { try { lock (ReadWriteLock) { XElement lastTiket = Doc.Elements().Descendants().Last(); string number = (long.Parse(lastTiket.Value) + 1).ToString(); //if number.lenght<18 fill with "0" at start while (number.Length < 18) { number = "0" + number; } //get 18 character in case number.lengh>18 string ticketNumber = number.Substring(number.Length - 18, 18); //add new ticket to xdocoment var newNode = new XElement("ValidVoucher", new XAttribute("status", TicketStatus.pending), new XAttribute("amount", amount)); newNode.SetValue(ticketNumber); if (Doc.Root != null) Doc.Root.Add(newNode); return ticketNumber; } } catch (Exception ex) { Logger.WriteLogger(ex, ConstValues.MainLogFileName); return "000000000000000000"; } } public static bool Reset() { try { lock (ReadWriteLock) { _doc = XDocument.Load(XmlFileName); } } catch (Exception ex) { Logger.WriteLogger(ex, ConstValues.MainLogFileName); return false; } return true; } } }