/// <exception cref="InvalidDataException"> /// The save format is corrupt and could not be loaded. /// </exception> private void Deserialize(XmlElement node) { IsUntitled = false; XmlElement nameElement = node["Name"]; if ((nameElement == null) || (nameElement.InnerText == "")) { throw new InvalidDataException("Project's name cannot be empty."); } name = nameElement.InnerText; foreach (XmlElement itemElement in node.GetElementsByTagName("ProjectItem")) { XmlAttribute typeAttribute = itemElement.Attributes["type"]; XmlAttribute assemblyAttribute = itemElement.Attributes["assembly"]; if ((typeAttribute == null) || (assemblyAttribute == null)) { throw new InvalidDataException("ProjectItem's type or assembly name is missing."); } string typeName = typeAttribute.InnerText; string assemblyName = assemblyAttribute.InnerText; try { Assembly assembly = Assembly.Load(assemblyName); IProjectItem projectItem = ( IProjectItem )assembly.CreateInstance(typeName, false, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, null, null, null); projectItem.Deserialize(itemElement); projectItem.Clean( ); Add(projectItem); } catch (InvalidDataException) { throw; } catch (Exception ex) { throw new InvalidDataException("Invalid type or assembly of ProjectItem.", ex); } } }
//build for clone, for the results display. //This is a deep copy way for the use of results set. //This use the xml serialization and deserialization. public Model Clone() { XmlDocument document = new XmlDocument(); XmlElement node = document.CreateElement("Model"); this.Serialize(node); Type type = this.GetType(); XmlAttribute typeAttribute = node.OwnerDocument.CreateAttribute("type"); typeAttribute.InnerText = type.FullName; node.Attributes.Append(typeAttribute); XmlAttribute assemblyAttribute = node.OwnerDocument.CreateAttribute("assembly"); assemblyAttribute.InnerText = type.Assembly.FullName; node.Attributes.Append(assemblyAttribute); string typeName = typeAttribute.InnerText; string assemblyName = assemblyAttribute.InnerText; try { Assembly assembly = Assembly.Load(assemblyName); IProjectItem projectItem = (IProjectItem)assembly.CreateInstance( typeName, false, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, null, null, null); projectItem.Deserialize(node); projectItem.Clean(); return((Model)projectItem); } catch (InvalidDataException) { throw; } catch (Exception ex) { throw new InvalidDataException("Invalid type or assembly of ProjectItem.", ex); } }