praktikum1/vr21/Program.cs

58 lines
1.7 KiB
C#
Raw Normal View History

2017-02-01 16:06:52 +02:00
using System;
2017-02-01 17:35:17 +02:00
using System.Collections.Generic;
2017-02-01 17:21:58 +02:00
using System.Xml.Linq;
2017-02-01 17:35:17 +02:00
using System.Xml.Serialization;
2017-02-01 16:06:52 +02:00
namespace vr21
{
2017-02-01 17:35:17 +02:00
public class Receipe
{
public string Name;
public List<Ingredient> Ingredients;
public Receipe()
{
Ingredients = new List<Ingredient>();
}
}
public class Ingredient
{
public string Name;
public int Count;
public string Unit;
}
2017-02-01 16:06:52 +02:00
class Program
{
2017-02-01 17:21:58 +02:00
private static string _xmllocation = @"..\..\..\XMLRecipe.xml";
2017-02-01 16:06:52 +02:00
static void Main(string[] args)
2017-02-01 17:35:17 +02:00
{
Ingredient kartul = new Ingredient() {Name = "Kartul", Count = 500, Unit = "g"};
Receipe r = new Receipe() {Name="Supp"};
r.Ingredients.Add(kartul);
r.Ingredients.Add(kartul);
List<Receipe> l = new List<Receipe>();
l.Add(r);
l.Add(r);
XmlSerializer x = new XmlSerializer(l.GetType());
x.Serialize(Console.Out, l);
//writeReceipesInformation();
2017-02-01 17:21:58 +02:00
Console.ReadKey();
}
2017-02-01 17:03:44 +02:00
2017-02-01 17:21:58 +02:00
static void writeReceipesInformation()
{
XDocument xdoc = XDocument.Load(_xmllocation);
var allReceipes = xdoc.Descendants("Recipe");
foreach (var receipe in allReceipes)
2017-02-01 17:03:44 +02:00
{
2017-02-01 17:21:58 +02:00
Console.WriteLine(receipe.Element("Name").Value);
foreach (var ingredient in receipe.Element("Ingredients").Elements())
2017-02-01 17:03:44 +02:00
{
2017-02-01 17:21:58 +02:00
Console.WriteLine($" {ingredient.Element("Name").Value}: {ingredient.Element("Amount").Value}{ingredient.Element("Unit").Value}");
2017-02-01 17:03:44 +02:00
}
}
2017-02-01 16:06:52 +02:00
}
}
}