예제 #1
0
파일: Deserializer.cs 프로젝트: Fihex/TPA
        public static void DeserializationTrader()
        {
            try
            {
                string mainFile = @"products.xml";
                if (File.Exists(mainFile))
                {
                    var doc = new XmlDocument();
                    doc.Load(mainFile);

                    var node = doc.SelectSingleNode("Products/Product");

                    if (node == null)
                    {
                        Console.Write("Press and choose [Y/N]\nY - to add\nN - to break\n");
                        var keyOne = System.Console.ReadKey(true);
                        switch (keyOne.Key)
                        {
                        case System.ConsoleKey.Y:
                            Serializer.Serialization();
                            break;

                        case System.ConsoleKey.N:
                            Console.WriteLine("\nBreak down...");
                            Console.ReadKey();
                            break;

                        default:
                            File.Delete(mainFile);
                            break;
                        }
                    }
                    else
                    {
                        XmlSerializer  xmlSerializer = new XmlSerializer(typeof(List <Product>), new XmlRootAttribute("Products"));
                        StreamReader   reader        = new StreamReader(mainFile);
                        List <Product> LProducts     = (List <Product>)xmlSerializer.Deserialize(reader);

                        Console.WriteLine();
                        foreach (Product ListProduct in LProducts)
                        {
                            Console.WriteLine($"Id: {ListProduct.Id}");
                            Console.WriteLine($"Category: {ListProduct.Category}");
                            Console.WriteLine($"Title: {ListProduct.Title}");
                            Console.WriteLine($"Price: {ListProduct.price.Value} {ListProduct.price.Unit}");
                            Console.WriteLine($"Weight: {ListProduct.description.Weight}");
                            Console.WriteLine();
                        }

                        reader.Close();

                        Console.WriteLine("Deserialization completed...");
                    }
                    Console.ReadKey();
                }
                else if (!File.Exists(mainFile))
                {
                    Console.Write("Press and choose [Y/N]\nY - to add\nN - to break\n");
                    var keyTwo = System.Console.ReadKey(true);
                    switch (keyTwo.Key)
                    {
                    case System.ConsoleKey.Y:
                        Serializer.Serialization();
                        break;

                    case System.ConsoleKey.N:
                        Console.WriteLine("\nBreak down...");
                        Console.ReadKey();
                        break;

                    default:
                        break;
                    }
                }
                else
                {
                    Console.WriteLine("Error");
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
예제 #2
0
파일: BuySell.cs 프로젝트: Fihex/TPA
        public static void RemoveProducts()
        {
            try
            {
                string mainFile = @"products.xml";
                if (!File.Exists(mainFile))
                {
                    Console.Write("File not found\nPress and choose [Y/N]\nY - to add\nN - to break\n");
                    var key = System.Console.ReadKey(true);
                    switch (key.Key)
                    {
                    case System.ConsoleKey.Y:
                        Serializer.Serialization();
                        break;

                    case System.ConsoleKey.N:
                        Console.WriteLine("\nBreak down...");
                        Console.ReadKey();
                        break;

                    default:
                        break;
                    }
                }
                else if (File.Exists(mainFile))
                {
                    Info      info = new Info();
                    XDocument xDoc = XDocument.Load(mainFile);

                    Console.Write("Id: ");
                    info.Id = Convert.ToInt32(Console.ReadLine());
                    Console.Write("Category: ");
                    info.Category = Console.ReadLine();

                    xDoc.Element("Products")
                    .Descendants("Product")
                    .Where(x => (string)x.Attribute("id") == "" + info.Id && (string)x.Attribute("category") == "" + info.Category)
                    .Remove();

                    xDoc.Save(mainFile);

                    while (true)
                    {
                        Console.Write("Press and choose [Y/N]\nY - to add\nN - to break\n");
                        var key = System.Console.ReadKey(true);
                        switch (key.Key)
                        {
                        case System.ConsoleKey.Y:

                            Console.Write("Id: ");
                            info.Id = Convert.ToInt32(Console.ReadLine());
                            Console.Write("Category: ");
                            info.Category = Console.ReadLine();

                            xDoc.Element("Products")
                            .Descendants("Product")
                            .Where(x => (string)x.Attribute("id") == "" + info.Id && (string)x.Attribute("category") == "" + info.Category)
                            .Remove();

                            xDoc.Save(mainFile);

                            continue;

                        case System.ConsoleKey.N:
                            Console.WriteLine("\nBreak down...");
                            Console.ReadKey();
                            break;

                        default:
                            break;
                        }
                        break;
                    }

                    var doc = new XmlDocument();
                    doc.Load(mainFile);

                    var node = doc.SelectSingleNode("Products/Product");

                    if (node == null)
                    {
                        File.Delete(mainFile);
                    }
                    else
                    {
                        Console.WriteLine("O_o");
                    }
                }
                else
                {
                    Console.WriteLine("Error");
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                Console.ReadKey();
            }
        }
예제 #3
0
 static void AddProducts()
 {
     Serializer.Serialization();
 }