Example #1
0
        private void FillClassFieldTree(ClassField classField)
        {
            var propertyInfos = classField.FieldType.GetProperties();

            if (classField.IsBasicType() || propertyInfos.Count() == 0)
            {
                return;
            }

            foreach (var propertyInfo in propertyInfos)
            {
                var xmlIgnoreAttr = propertyInfo.GetCustomAttributes(typeof(XmlIgnoreAttribute), true);
                if (xmlIgnoreAttr.Any()) continue;

                FillClassFieldTreeWithProperties(classField, propertyInfo);
            }
        }
Example #2
0
        private void TraverseClassFieldTree(XmlDocument xmlDocument, XmlElement xmlElement, ClassField classField)
        {
            var fieldLabel = classField.FieldName;
            if (!string.IsNullOrEmpty(fieldLabel))
            {
                var splitted = fieldLabel.Split(new char[] { '.' });
                fieldLabel = splitted[splitted.Count() - 1];
            }

            var element = xmlDocument.CreateElement(fieldLabel);

            if (xmlElement == null)
            {
                xmlDocument.AppendChild(element);
            }
            else
            {
                xmlElement.AppendChild(element);
            }

            if (classField.IsBasicType() || classField.FieldType.IsEnum)
            {

                var htmlEncoding = new HtmlEncoding();
                element.InnerText = classField.FieldValue == null ? "" : htmlEncoding.HtmlEncode(classField.FieldValue.ToString());

                return;
            }

            var traverseFields = true;
            if ((classField.FieldType.IsArray || classField.FieldType.GetGenericArguments().Count() > 0) && classField.FieldValue != null)
            {
                traverseFields = false;
                var typeHelper = new TypeHelper();
                var childFieldValues = (IList)classField.FieldValue;
                foreach (var childFieldValue in childFieldValues)
                {
                    var isBasicType = typeHelper.IsBasicType(childFieldValue.GetType());
                    if (!isBasicType)
                    {
                        traverseFields = true;
                        break;
                    }
                    var subChildField = new ClassField(childFieldValue.GetType().Name, classField.Fields[0].FieldType, childFieldValue);
                    TraverseClassFieldTree(xmlDocument, element, subChildField);
                }
            }
            if (!traverseFields)
            {
                return;
            }
            foreach (var field in classField.Fields)
            {
                TraverseClassFieldTree(xmlDocument, element, field);
            }
        }