SegmentItemViewModel DeSerialize(XmlDocument xml) { SegmentItemViewModel model = new SegmentItemViewModel(); var collection = model.GetType().GetProperties(); foreach (var item in collection) { XmlNodeList nodelist = xml.GetElementsByTagName(item.Name); if (nodelist.Count > 0) { if (item.CanWrite) { item.SetValue(model, nodelist[0].InnerText); } } } var snippets = xml.GetElementsByTagName("Snippet"); if (snippets == null || snippets.Count == 0) { return(model); } model.Snippet = xml.GetElementsByTagName("Snippet")[0].OuterXml; model.Snippet = FormatXml(model.Snippet); return(model); }
public void Save(SegmentItemViewModel model, string path) { string formatStr = Properties.Resources.SegmentTemplate; string text = string.Format(formatStr, model.Title, model.Shortcut, model.Description, model.Author, model.SnippetTypes, model.Snippet); File.WriteAllText(path, text); }
public SegmentItemViewModel Create(string path) { XmlDocument xml = new XmlDocument(); xml.Load(path); SegmentItemViewModel segment = this.DeSerialize(xml); segment.FileName = Path.GetFileNameWithoutExtension(path); segment.FilePath = path; return(segment); }
XmlDocument Serialize(SegmentItemViewModel model) { XmlDocument xml = new XmlDocument(); XmlNode codesnippet = xml.CreateElement("CodeSnippet"); xml.AppendChild(codesnippet); #region - header- XmlNode header = xml.CreateElement("Header"); codesnippet.AppendChild(header); XmlNode title = xml.CreateElement("Title"); header.AppendChild(title); XmlNode shortcut = xml.CreateElement("Shortcut"); header.AppendChild(shortcut); XmlNode description = xml.CreateElement("Description"); header.AppendChild(description); XmlNode author = xml.CreateElement("Author"); header.AppendChild(author); XmlNode snippettypes = xml.CreateElement("SnippetTypes"); header.AppendChild(snippettypes); XmlNode snippettype = xml.CreateElement("SnippetType"); snippettypes.AppendChild(snippettype); #endregion #region - snippet - XmlNode snippet = xml.CreateElement("Snippet"); codesnippet.AppendChild(snippet); #endregion var collection = model.GetType().GetProperties(); foreach (var item in collection) { XmlNodeList nodelist = xml.GetElementsByTagName(item.Name); if (nodelist.Count > 0) { if (item.CanWrite) { nodelist[0].InnerText = item.GetValue(model).ToString(); } } } return(xml); }