NClass.Core.Models.ClassModel.LoadRelationships C# (CSharp) Method

LoadRelationships() protected method

/// The save format is corrupt and could not be loaded. /// /// is null. ///
protected LoadRelationships ( XmlNode root ) : void
root System.Xml.XmlNode
return void
		protected override void LoadRelationships(XmlNode root)
        {
            if (root == null)
                throw new ArgumentNullException("root");

            XmlNodeList nodeList = root.SelectNodes(
                "Relationships/Relationship|Relations/Relation"); // old file format

            foreach (XmlElement node in nodeList)
            {
                string type = node.GetAttribute("type");
                string firstString = node.GetAttribute("first");
                string secondString = node.GetAttribute("second");
                int firstIndex, secondIndex;

                if (!int.TryParse(firstString, out firstIndex) ||
                    !int.TryParse(secondString, out secondIndex))
                {
                    throw new InvalidDataException(Strings.ErrorCorruptSaveFormat);
                }
                if (firstIndex < 0 || firstIndex >= entities.Count ||
                    secondIndex < 0 || secondIndex >= entities.Count)
                {
                    throw new InvalidDataException(Strings.ErrorCorruptSaveFormat);
                }

                try
                {
                    IEntity first = entities[firstIndex];
                    IEntity second = entities[secondIndex];
                    Relationship relationship;

                    switch (type)
                    {
                        case "Association":
                            relationship = AddAssociation(first as TypeBase, second as TypeBase);
                            break;

                        case "Generalization":
                            relationship = AddGeneralization(
                                first as CompositeType, second as CompositeType);
                            break;

                        case "Realization":
                            relationship = AddRealization(first as TypeBase, second as InterfaceType);
                            break;

                        case "Dependency":
                            relationship = AddDependency(first as TypeBase, second as TypeBase);
                            break;

                        case "Nesting":
                            relationship = AddNesting(first as CompositeType, second as TypeBase);
                            break;

                        case "Comment":
                        case "CommentRelationship": // Old file format
                            if (first is Comment)
                                relationship = AddCommentRelationship(first as Comment, second);
                            else
                                relationship = AddCommentRelationship(second as Comment, first);
                            break;

                        default:
                            throw new InvalidDataException(
                                Strings.ErrorCorruptSaveFormat);
                    }
                    relationship.Deserialize(node);
                }
                catch (ArgumentNullException ex)
                {
                    throw new InvalidDataException("Invalid relationship.", ex);
                }
                catch (RelationshipException ex)
                {
                    throw new InvalidDataException("Invalid relationship.", ex);
                }
            }
        }