Hello i have another question. First of all i have following classes:
{
[DataMember]
[XmlElement(ElementName = "RecipeBook")]
public Dictionary<string, Recipe> DRezeptbuch = new Dictionary<string, Recipe>();
}
public class Recipe
{
[XmlElement(ElementName = "Name")]
public string Name { get; set; }
[XmlElement(ElementName = "Size")]
public int? Size { get; set; }
[XmlElement(ElementName = "Ingredients")]
public List<Ingredient> Ingredients { get; set; }
}
public class Ingredient
{
[XmlElement(ElementName = "Name")]
public string Name { get; set; }
[XmlElement(ElementName = "Amount")]
public decimal amount;
[XmlElement(ElementName = "Unit")]
public Units.Unit unit;
[XmlElement(ElementName = "Specification")]
public List<Units.Specification> specification;
[XmlElement(ElementName = "Category")]
public Units.Category category;
}
So im filling the "DRezeptbuch" and want to write this to a file. I tried xmlserialization but i failed.
//var serializer = new XmlSerializer(dRezeptbuch.GetType());
//using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
//{
// serializer.Serialize(ms, dRezeptbuch);
// ms.Position = 0;
// xmlDoc.Load(ms);
// return xmlDoc.InnerXml;
//}
Then i tried it with this DataContract thingy but i wont work either.
var serializer = new DataContractSerializer(typeof(RecipeBook));
string xmlString;
using (var sw = new StringWriter())
{
using (var writer = new XmlTextWriter(sw))
{
writer.Formatting = Formatting.Indented; // indent the Xml so it's human readable
serializer.WriteObject(writer, dRezeptbuch);
writer.Flush();
xmlString = sw.ToString();
}
}
so what should i do? is there a good way to save this dictionary?