private static Project LoadWithPreviousFormat(XmlElement root) { Project project = new Project(); project.loading = true; Assembly assembly = Assembly.Load("EnClass.DiagramEditor"); IProjectItem projectItem = (IProjectItem)assembly.CreateInstance( "EnClass.DiagramEditor.ClassDiagram.Diagram", false, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, null, null, null); try { projectItem.Deserialize(root); } catch (Exception ex) { throw new InvalidDataException(Strings.ErrorCorruptSaveFile, ex); } project.Add(projectItem); project.loading = false; project.isReadOnly = true; return(project); }
private static Project LoadWithPreviousFormat(XmlElement root) { Project project = new Project(); project.loading = true; Assembly assembly = Assembly.Load("GBE.TemplateDesigner"); IProjectItem projectItem = (IProjectItem)assembly.CreateInstance( "GBE.TemplateDesigner.QueryTemplate.Template", false, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, null, null, null); try { projectItem.Deserialize(root); } catch (Exception ex) { throw new InvalidDataException("The save file is corrupt and could not be loaded.", ex); } project.Add(projectItem); project.loading = false; project.isReadOnly = true; return(project); }
/// <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; // This is some compatability fix with older file formats, pre 2.5 const string oldTypeName = "NClass.DiagramEditor.ClassDiagram.Diagram"; const string newTypeName = "NClass.DiagramEditor.ClassDiagram.ClassDiagram"; var realAssemblyName = new AssemblyName(assemblyName); if (realAssemblyName.Version < new Version(2, 5) && string.CompareOrdinal(oldTypeName, typeName) == 0) { typeName = newTypeName; } 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); } } }
/// <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); } }