Exemplo n.º 1
0
        public ClassInfo GenerateDataClass(bool preserveHierarchy)
        {
            ClassInfo info = new ClassInfo("public partial", ClassName, null, string.Format("In memory representation of the XML element \"{0}\".", Name));
            info.AddUsing("System.Xml");
            info.AddUsing("System.IO");

            List<DataInfo> dataList = new List<DataInfo>();

            // Add text property.
            if (Text.Include)
                dataList.Add(Text.Info);

            // Add CDATA property.
            if (CDATA.Include)
                dataList.Add(CDATA.Info);

            // Add properties to represent the attributes.
            foreach (AttributeInfo attrib in Attributes)
                dataList.Add(attrib.Info);

            foreach(DataInfo data in dataList)
            {
                StringBuilder sb = new StringBuilder();
                sb.Append(string.Format("Gets or sets the value of the child {0} element.", data.Name));
                if(data.SelectedDataType == DataType.String)
                {
                    if (data.IsOptional)
                        sb.Append(" Can be null.");
                    if (data.CanBeEmpty)
                        sb.Append(" Can be empty.");
                }
                else
                {
                    if (data.IsOptional || data.CanBeEmpty)
                        sb.Append(" Can be null.");
                }

                info.Properties.Add(new PropertyInfo("public", data.GetDataTypeString(), data.PropertyName, sb.ToString()));

                // Add any additional properties.
                info.Properties.AddRange(data.SelectedDataTypeObject.GenerateAdditionalProperties());

                // Add any additional enums.
                info.Enums.AddRange(data.SelectedDataTypeObject.GenerateAdditionalEnums());

                // Add any custom usings.
                info.AddUsings(data.GetSelectedUsings());
            }

            // Add properties to represent the child nodes.
            foreach (ElementInfo child in Children)
            {
                string summary = "Gets or sets the child XML elements.";
                info.Properties.Add(new PropertyInfo("public", string.Format("{0}[]", child.ClassName), GenerateChildArrayNameProperty(child.ClassName), summary, null, null, "private"));
            }

            // Create the constructors.
            info.Constructors.Add(GenerateDataClassConstructor());
            info.Constructors.Add(GenerateDataClassXmlNodeConstructor());

            // Add additional methods.
            info.Methods.AddRange(GenerateMethods(dataList.ToArray()));
            info.Methods.Add(GenerateCreateElementMethod());

            if(preserveHierarchy)
            {
                // Add additional sub-classes.
                foreach (ElementInfo child in Children)
                {
                    ClassInfo childClass = child.GenerateDataClass(preserveHierarchy);
                    info.AddChildClass(childClass);
                }
            }

            return info;
        }