public static string MarkToXml(Mark mark) { Dictionary<string, object> data = new Dictionary<string, object>(); data.Add("_class", mark._class); data.Add("name", mark.name); data.Add("date", mark.date); data.Add("mark", mark.mark); data.Add("note", mark.note); StringBuilder xml = new StringBuilder(); xml.AppendLine(" <mark>"); foreach (KeyValuePair<string, object> item in data) { xml.AppendLine(String.Format(" <item key=\"{0}\">{1}</item>", item.Key, item.Value.ToString())); } foreach (float f in mark.points) { xml.AppendLine(String.Format(" <point>{0}</point>", f)); } xml.AppendLine(" </mark>"); return xml.ToString(); }
/// <summary> /// Happens when you click the "Add mark" button /// </summary> private void button_mark_add_Click(object sender, EventArgs e) { if (textBox_mark_mark.Text == "") { Data.messageBox("Warnung", "Es wurde keine note eingetragen!"); } else { string _class; try { if (comboBox_mark_classes.Text == "") { Data.messageBox("Warnung", "Kein Fach gewählt"); return; } else { _class = comboBox_mark_classes.Text; } } catch { Data.messageBox("Warnung", "Kein Fach gewählt!"); return; } string _name = textBox_mark_name.Text; DateTime _date = Convert.ToDateTime(dateTimePicker_mark.Text); float _mark = Convert.ToSingle((textBox_mark_mark.Text != "" ? textBox_mark_mark.Text : "0")); float _pointsGot = Convert.ToSingle((textBox_mark_points.Text != "" ? textBox_mark_mark.Text : "0")); float _pointsMax = Convert.ToSingle((textBox_mark_mpoints.Text != "" ? textBox_mark_mark.Text : "0")); string _note = textBox_mark_note.Text; Mark m = new Mark(_class,_name,_date,_mark,_pointsGot, _pointsMax,_note); Data.marks.Add(m); UpdateMarks(); button_mark_clear_Click(null,null); } }
public static string MarkToXml(Mark mark) { //Load type Type t = typeof(Mark); //Init builder StringBuilder xml = new StringBuilder(); //Start mark node xml.AppendLine("\t<mark>"); //Loop through each property in class foreach (var property in t.GetProperties()) { //Store property in xml xml.AppendLine( String.Format("\t\t<item key=\"{0}\">{1}</item>", //Name of property property.Name, //Gets value from instance and converts to string property.GetValue(mark,null).ToString())); } xml.AppendLine("\t</mark>"); return xml.ToString(); }
public static List<Mark> XmlToMark(string file) { FileStream xmlfs = new FileStream(file, FileMode.Open); XmlReader xmlr = XmlReader.Create(xmlfs); List<Mark> loaded = new List<Mark>(); Mark current = null; List<float> cpoints = new List<float>(); while (xmlr.Read()) { if(xmlr.Name == "mark" && xmlr.NodeType == XmlNodeType.Element) { current = new Mark("","",DateTime.Now,0,new float[0],""); cpoints = new List<float>(); } else if(xmlr.Name == "item" && xmlr.NodeType == XmlNodeType.Element) { string nodekey = xmlr.GetAttribute("key"); var properties = current.GetType().GetProperties(); Type ptype = current.GetType().GetProperty(nodekey).PropertyType; current.GetType().GetProperty(nodekey).SetValue(current, Convert.ChangeType(xmlr.ReadElementContentAsString(), ptype), null); } else if (xmlr.Name == "point" && xmlr.NodeType == XmlNodeType.Element) { cpoints.Add(Convert.ToSingle(xmlr.ReadElementContentAsString())); } else if (xmlr.Name == "mark" && xmlr.NodeType == XmlNodeType.EndElement) { current.points = cpoints.ToArray(); loaded.Add(current); } } xmlfs.Close(); return loaded; }
public static List<Mark> XmlToMark(string file) { //Check if file exists if (File.Exists(file)) { //-------------Read file--------------------- //Open streams FileStream xmlfs = new FileStream(file, FileMode.Open); XmlReader xmlr = XmlReader.Create(xmlfs); //Create empty list to hold loaded marks List<Mark> loaded = new List<Mark>(); //Initialize storing vars Mark current = null; //Begin read loop while (xmlr.Read()) { if (xmlr.Name == "mark" && xmlr.NodeType == XmlNodeType.Element) { current = new Mark("", "", DateTime.Now, 0, 0, 0, ""); } else if (xmlr.Name == "item" && xmlr.NodeType == XmlNodeType.Element) { //Get property name string nodekey = xmlr.GetAttribute("key"); //load properties in class Type ptype = current.GetType().GetProperty(nodekey).PropertyType; //Set property current.GetType().GetProperty(nodekey).SetValue( //Store in current current, //Cast data in element to property type Convert.ChangeType( xmlr.ReadElementContentAsString(), ptype), //Not used, only for indexed properties. Use null null); } else if (xmlr.Name == "mark" && xmlr.NodeType == XmlNodeType.EndElement) { loaded.Add(current); } } xmlfs.Close(); return loaded; } else { return null; } }