public static NodeComponent FromXml(XElement component) { Type T; if (ComponentMap.TryGetValue(component.Element("ComponentName").Value, out T)) { Object o = Activator.CreateInstance(T); if (!(o is NodeComponent)) { throw new Exception("Attempt to deserialize XML failed. Object of type " + o.GetType().Name + " could not be cast to NodeComponent"); } if (ComponentFields.ContainsKey(T)) { // for each Property element in xml, try to find the matching component variable foreach (XElement p in component.Elements("Property")) { foreach (FieldInfo fi in ComponentFields[T]) { if (p.Element("Key").Value == fi.Name) { fi.SetValue(o, p.Element("Value").Value); } } } } if (component.Element("ComponentNodeLink") != null) { (o as NodeComponent).nodeLinks = component .Elements("ComponentNodeLink") .Select(e => new ComponentNodeLink(e)) .ToList(); } return(o as NodeComponent); } else { throw new Exception("Attempt to deserialize XML failed. Component of type " + component.Element("ComponentName").Value + " could not be found in component map"); } }
public XElement ToXml() { XElement node = new XElement("Component"); node.Add(new XElement("ComponentName", GetType().ToString())); if (ComponentFields.ContainsKey(GetType())) { foreach (FieldInfo fi in ComponentFields[GetType()]) { XElement propertyNode = new XElement("Property"); propertyNode.Add(new XElement("Key", fi.Name)); propertyNode.Add(new XElement("Value", fi.GetValue(this))); node.Add(propertyNode); } } foreach (ComponentNodeLink l in nodeLinks) { node.Add(l.ToXml()); } return(node); }
/// <summary> /// Add field to be searched in the More Like This component/widget /// <para>Default value is "Name"</para> /// </summary> public void AddComponentField(string name) { ComponentFields.TryAdd(name, true); }