NClass.Core.Project.Deserialize C# (CSharp) Method

Deserialize() private method

/// The save format is corrupt and could not be loaded. ///
private Deserialize ( XmlElement node ) : void
node System.Xml.XmlElement
return void
		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);
				}
			}
		}

Usage Example

コード例 #1
0
ファイル: Project.cs プロジェクト: xiaoxiongnpu/NClass
        /// <exception cref="IOException">
        /// Could not load the project.
        /// </exception>
        /// <exception cref="InvalidDataException">
        /// The save file is corrupt and could not be loaded.
        /// </exception>
        /// <exception cref="ArgumentException">
        /// <paramref name="fileName"/> is empty string.
        /// </exception>
        public static Project Load(string fileName)
        {
            if (string.IsNullOrEmpty(fileName))
            {
                throw new ArgumentException(Strings.ErrorBlankFilename, "fileName");
            }

            if (!File.Exists(fileName))
            {
                throw new FileNotFoundException(Strings.ErrorFileNotFound);
            }

            XmlDocument document = new XmlDocument();

            try
            {
                document.Load(fileName);
            }
            catch (Exception ex)
            {
                throw new IOException(Strings.ErrorCouldNotLoadFile, ex);
            }

            XmlElement root = document["Project"];

            if (root == null)
            {
                root = document["ClassProject"];                 // Old file format
                if (root == null)
                {
                    throw new InvalidDataException(Strings.ErrorCorruptSaveFile);
                }
                else
                {
                    Project oldProject = LoadWithPreviousFormat(root);
                    oldProject.FilePath   = fileName;
                    oldProject.name       = Path.GetFileNameWithoutExtension(fileName);
                    oldProject.isUntitled = false;
                    return(oldProject);
                }
            }

            Project project = new Project();

            project.loading = true;
            try
            {
                project.Deserialize(root);
            }
            catch (Exception ex)
            {
                throw new InvalidDataException(Strings.ErrorCorruptSaveFile, ex);
            }
            project.loading    = false;
            project.FilePath   = fileName;
            project.isReadOnly = project.projectFile.IsReadOnly;

            return(project);
        }
All Usage Examples Of NClass.Core.Project::Deserialize