public void UpdateUserCart(Cart updatedUserCart) { Directory.CreateDirectory(_cartsXmlPath); var userCartPath = $"{_cartsXmlPath + updatedUserCart.UserId}.xml"; XmlSerializerTool.InsertSerializedObject(userCartPath, updatedUserCart); }
public Cart GetUserCart(int userId) { Directory.CreateDirectory(_cartsXmlPath); var userCartPath = $"{_cartsXmlPath + userId}.xml"; if (!File.Exists(userCartPath)) { throw new Exception(MessageResource.UnavailableUserCart); } return(XmlSerializerTool.DeserializeObjectList <Cart>(userCartPath)); }
public void IncreaseProductQuantity(int productId, int countToIncrease) { var products = GetAllProducts().ToList(); var product = products.SingleOrDefault(x => x.Id == productId); if (product == null) { throw new Exception(MessageResource.UnavailableProduct); } product.Count += countToIncrease; XmlSerializerTool.InsertSerializedObject(_productsXmlPath, products); }
public static bool WriteLicense(LocalLicenseData licData, string fileName) { try { string tmp = Path.GetTempFileName(); XmlSerializerTool <LocalLicenseData> bs = new XmlSerializerTool <LocalLicenseData>(); bs.ToFile(licData, tmp); CryptoHelp.EncryptFile(tmp, fileName, "a89sd8jkk34dm%^%1239"); return(true); } catch (Exception) { return(false); } }
public void DecreaseProductQuantity(int productId, int countToDecrease) { var products = GetAllProducts().ToList(); var product = products.SingleOrDefault(x => x.Id == productId); if (product == null) { throw new Exception(MessageResource.UnavailableProduct); } if (product.Count < countToDecrease) { throw new Exception(MessageResource.AddToCartWithAnOutOfStockItemError); } product.Count -= countToDecrease; XmlSerializerTool.InsertSerializedObject(_productsXmlPath, products); }
public static LocalLicenseData ReadLicense(string fileName) { try { if (!File.Exists(fileName)) { return(null); } string tmp = Path.GetTempFileName(); CryptoHelp.DecryptFile(fileName, tmp, "a89sd8jkk34dm%^%1239"); XmlSerializerTool <LocalLicenseData> bs = new XmlSerializerTool <LocalLicenseData>(); LocalLicenseData licData = (LocalLicenseData)bs.FromFile(tmp); return(licData); } catch (Exception e) { return(null); } }
/// <summary> /// 序列化测试 /// </summary> public static void SerializerTest() { WriteLine(); WriteColorText("SerializerTest", ConsoleColor.Green); WriteColorText("--------------------------------------------------------------------------------------------------------------------", ConsoleColor.Yellow); WriteLine($"Xml序列化:"); Student z_san = new() { Name = "张三", Sex = "女", Age = 23 }; var z_san_str = XmlSerializerTool.ToXml(z_san); WriteColorText(XmlSerializerTool.FormatXML(z_san_str), ConsoleColor.Magenta); WriteLine("Binary序列化:"); Student li_si = new() { Name = "李四", Sex = "男", Age = 25 }; var li_si_str = BinarySerializerTool.ToBinary(li_si); WriteColorText(li_si_str, ConsoleColor.Magenta); WriteLine($"Xml反序列化:"); var z_s = XmlSerializerTool.FromXml <Student>(z_san_str); z_s.ToConsole(); WriteLine("Binary反序列化:"); var lis = BinarySerializerTool.FromBinary <Student>(li_si_str); lis.ToConsole(); WriteColorText("--------------------------------------------------------------------------------------------------------------------", ConsoleColor.Yellow); WriteColorText("SerializerTest Complete", ConsoleColor.Green); WriteLine(); } } }
public IList <Product> GetAllProducts() { var products = XmlSerializerTool.DeserializeObjectList <ProductsCollection>(_productsXmlPath).Products; return(products.Where(x => x.Count > 0).ToList()); //return only available products that has stocks }