praktikum1/vr21/Program.cs

104 lines
3.1 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using System.Xml.Serialization;
using System.IO;
using System.Xml;
namespace vr21
{
public class Recipe
{
public string Name;
public string Description;
public List<Ingredient> Ingredients;
public List<string> Steps;
public Recipe()
{
Ingredients = new List<Ingredient>();
Steps = new List<string>();
}
}
public class Ingredient
{
public string Name;
public int Amount;
public string Unit;
}
class Program
{
// private static string _xmllocation = @"..\..\..\XMLRecipe.xml";
private static string _xmllocation = @"XMLRecipe.xml";
public static List<Recipe> Recipes = new List<Recipe>();
static void Main(string[] args)
{
Ingredient kartul = new Ingredient() {Name = "Kartul", Amount = 500, Unit = "g"};
Recipe r = new Recipe() {Name="Supp"};
r.Ingredients.Add(kartul);
r.Ingredients.Add(kartul);
r.Steps.Add("Koori");
r.Steps.Add("Keeda");
var l = Recipes;
l.Add(r);
l.Add(r);
r = new Recipe() {Name="kala"};
l.Add(r);
XmlSerializer x = new XmlSerializer(l.GetType());
using (var fs = new FileStream(_xmllocation, FileMode.Create)) {
x.Serialize(fs, l);
}
//writeReceipesInformation();
readRecipesInformation();
System.Environment.Exit(1);
string s;
while ((s = Console.ReadLine()) != null)
{
var arguments = s.Split(' ');
var command = arguments[0].Trim();
switch (command.ToLower())
{
case "list":
Console.WriteLine("Tere");
break;
default:
Console.WriteLine("No command");
break;
}
}
}
static void readRecipesInformation() {
var x = new XmlSerializer(typeof(List<Recipe>));
FileStream fs = new FileStream(_xmllocation, FileMode.Open);
XmlReader reader = XmlReader.Create(fs);
var l = (List<Recipe>)x.Deserialize(reader);
foreach (var r in l) {
Console.WriteLine(r.Name);
}
}
static void writeReceipesInformation()
{
XDocument xdoc = XDocument.Load(_xmllocation);
var allReceipes = xdoc.Descendants("Recipe");
Console.WriteLine(allReceipes.Count());
foreach (var receipe in allReceipes)
{
Console.WriteLine(receipe.Element("Name").Value);
foreach (var ingredient in receipe.Element("Ingredients").Elements())
{
Console.WriteLine($" {ingredient.Element("Name").Value}: {ingredient.Element("Amount").Value}{ingredient.Element("Unit").Value}");
}
}
}
}
}