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 RecipeList { get; set; } public Recipes() { RecipeList = new List(); } } public class Recipe { [XmlElement("Name")] public string Name { get; set; } [XmlElement("Ingredient")] public List IngredientsList { get; set; } [XmlElement("Step")] public List StepList { get; set; } public Recipe() { IngredientsList = new List(); StepList = new List(); } public Recipe(string name) { Name = name; IngredientsList = new List(); StepList = new List(); } public Recipe(string name, List ingredients) { Name = name; IngredientsList = ingredients; StepList = new List(); } } 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); } } }