public ElementInfo(XmlNode[] nodes) { if (nodes == null) throw new ArgumentNullException("nodes"); if (nodes.Length == 0) throw new ArgumentException("nodes is an empty array"); // Verify all the nodes are elements and have the same element name. string elementName = null; foreach(XmlNode node in nodes) { if (node.NodeType != XmlNodeType.Element) throw new ArgumentException("nodes contained an XmlNode that was not an element node type."); if (elementName == null) { elementName = node.Name; } else { if (string.Compare(elementName, node.Name, false) != 0) throw new ArgumentException("One or more of the nodes provided do not contain the same element name"); } } Name = elementName; ClassName = StringUtility.GetUpperCamelCase(elementName); // Parse all the attribute names in the nodes. string[] attribNames = AttributeInfo.GetAllAttributeNames(nodes); // Determine the attributes. Attributes = new AttributeInfo[attribNames.Length]; for (int i = 0; i < attribNames.Length; i++) Attributes[i] = new AttributeInfo(attribNames[i], nodes); // Verify that none of the attribute property names match the element class name. foreach(AttributeInfo attrib in Attributes) { // If the attribute does match then change the name. if(string.Compare(attrib.Info.PropertyName, ClassName, false) == 0) attrib.Info.PropertyName = string.Format("{0}Attribute", attrib.Info.PropertyName); } // Verify that no two attribute names have the same property name. List<string> propertyNames = new List<string>(Attributes.Length); foreach(AttributeInfo attrib in Attributes) { string propertyName = attrib.Info.PropertyName; int index = 1; string newName = propertyName; while (propertyNames.Contains(newName)) { newName = string.Format("{0}{1}", propertyName, index); index++; } if (newName != attrib.Info.PropertyName) attrib.Info.PropertyName = newName; propertyNames.Add(newName); } // CDATA CDATA = new CDataInfo(nodes); // Verify that none of the attribute names match the CDATA property. if (CDATA.Include) { foreach (AttributeInfo attrib in Attributes) { // If the attribute does match this then change the name. if (string.Compare(attrib.Info.PropertyName, CDATA.Info.PropertyName, false) == 0) attrib.Info.PropertyName = string.Format("{0}Attribute", attrib.Info.PropertyName); } } // Text Text = new TextInfo(nodes); if(Text.Include) { // Verify that none of the attribute names match the text property. foreach(AttributeInfo attrib in Attributes) { // If the attribute does match this then change the name. if (string.Compare(attrib.Info.PropertyName, Text.Info.PropertyName, false) == 0) attrib.Info.PropertyName = string.Format("{0}Attribute", attrib.Info.PropertyName); } } }
/// <summary> /// Adds a leaf node. /// </summary> /// <param name="node">Node to be added.</param> /// <param name="attrib"><see cref="AttributeInfo"/> corresponding to the node.</param> private void AddLeafNode(TreeNode node, AttributeInfo attrib) { int index = node.Nodes.Add(new TreeNode(attrib.Info.Name)); node.Nodes[index].Tag = attrib; }