praktikum1/vr21/Program.cs

100 lines
2.3 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Serialization;
using Microsoft.SqlServer.Server;
namespace vr21
{
[XmlRoot("Recipes")]
public class Recipes
{
[XmlElement("Recipe")]
public List<Recipe> RecipeList { get; set; }
public Recipes()
{
RecipeList = new List<Recipe>();
}
}
public class Recipe
{
[XmlElement("Name")]
public string Name { get; set; }
[XmlElement("Ingredient")]
public List<Ingredient> IngredientsList { get; set; }
[XmlElement("Step")]
public List<string> StepList { get; set; }
public Recipe()
{
IngredientsList = new List<Ingredient>();
StepList = new List<string>();
}
public Recipe(string name)
{
Name = name;
IngredientsList = new List<Ingredient>();
StepList = new List<string>();
}
public Recipe(string name, List<Ingredient> ingredients)
{
Name = name;
IngredientsList = ingredients;
StepList = new List<string>();
}
}
public class Ingredient
{
[XmlElement("Name")]
public string Name { get; set; }
[XmlElement("Amount")]
public int Amount { get; set; }
public Ingredient(string name)
{
Name = name;
Amount = 1;
}
public Ingredient(string name, int amount)
{
Name = name;
Amount = amount;
}
}
class Program
{
static void Main(string[] args)
{
var recipes = new Recipes();
recipes.RecipeList.Add(new Recipe("Supp"));
XmlSerializer serializer = new XmlSerializer(typeof(Recipes));
var xml = "";
using (var sww = new StringWriter())
{
using (XmlWriter writer = XmlWriter.Create(sww))
{
serializer.Serialize(writer, recipes);
xml = sww.ToString(); // Your XML
}
}
Console.WriteLine(xml);
}
}
}