public static XRoot ParseXML(string rawxml) { var xml = new XmlDocument(); xml.LoadXml(rawxml); var root = new XRoot(); foreach (XmlNode XTemplate in xml.ChildNodes[0].ChildNodes) { if (XTemplate.Name != "template") { continue; } if (XTemplate.ChildNodes.Count > 0) { var template = new XTemplate(); foreach (XmlNode child in XTemplate.ChildNodes) { if (child.Name == "title") { template.Title = child.InnerText; } else if (child.Name == "part") { var part = new XPart { Name = child.FirstChild.InnerText, // sorry Value = child.LastChild.InnerText }; template.Parts.Add(part); } } root.Templates.Add(template); } } return(root); }
public static Template Translate(XTemplate xtemplate) { foreach (var type in templateTypes) { var titleProp = type.GetProperty(nameof(Template.Title)); var templateTitle = titleProp.GetValue(null) as string; if (xtemplate.Title == templateTitle) { var instance = Activator.CreateInstance(type); foreach (var part in xtemplate.Parts) { foreach (var prop in type.GetProperties()) { if (prop.Name.ToLowerInvariant() == part.Name.ToLowerInvariant()) { prop.SetValue(instance, part.Value); } } } return(instance as Template); } } throw new Exception("Unsupported template type: " + xtemplate.Title); }