Exemple #1
0
        /// <summary>
        /// XmlObject 将object序列化为xml字符串
        /// </summary>
        public static string XmlObject(object obj)
        {
            XmlDocument xmlDoc  = new XmlDocument();
            XmlElement  objNode = xmlDoc.CreateElement("object");

            SpringFox.SetPropertyValue(objNode, "type", TypeHelper.GetTypeRegularName(obj.GetType()), XmlPropertyPosition.Attribute);
            SpringFox.DoXmlObject(obj, xmlDoc, objNode);

            return(objNode.OuterXml);
        }
Exemple #2
0
        /// <summary>
        /// DoXmlObject 根据obj的各个属性创建对应的Node作为curNode的子节点
        /// </summary>
        private static void DoXmlObject(object obj, XmlDocument xmlDoc, XmlNode curNode)
        {
            try
            {
                if (TypeHelper.IsSimpleType(obj.GetType()))
                {
                    return;
                }

                #region logic
                foreach (PropertyInfo pro in obj.GetType().GetProperties())
                {
                    if ((!pro.CanRead))
                    {
                        continue;
                    }

                    object[] nonXmlAttrs = pro.GetCustomAttributes(typeof(NonXmlAttribute), true);
                    if ((nonXmlAttrs != null) && (nonXmlAttrs.Length > 0))
                    {
                        continue;
                    }

                    object proVal = pro.GetValue(obj, null);
                    if (proVal == null)
                    {
                        continue;
                    }

                    if (TypeHelper.IsSimpleType(pro.PropertyType))
                    {
                        SpringFox.SetPropertyValue(curNode, pro.Name, proVal.ToString(), XmlPropertyPosition.ChildNode);
                    }
                    else
                    {
                        bool isGenericList = pro.PropertyType.IsGenericType && (pro.PropertyType.GetGenericTypeDefinition() == typeof(IList <>));

                        if (isGenericList)
                        {
                            #region IList<>
                            XmlElement proNode = xmlDoc.CreateElement("property");
                            SpringFox.SetPropertyValue(proNode, "name", pro.Name, XmlPropertyPosition.Attribute);
                            curNode.AppendChild(proNode);

                            Type listElementType = pro.PropertyType.GetGenericArguments()[0];

                            XmlElement listNode = xmlDoc.CreateElement("list");
                            SpringFox.SetPropertyValue(listNode, "element-type", TypeHelper.GetTypeRegularName(listElementType), XmlPropertyPosition.Attribute);
                            proNode.AppendChild(listNode);

                            if (TypeHelper.IsSimpleType(listElementType))
                            {
                                foreach (object element in (IEnumerable)proVal)
                                {
                                    XmlHelper.SetPropertyValue(listNode, "value", element.ToString(), XmlPropertyPosition.ChildNode, false);
                                }
                            }
                            else
                            {
                                foreach (object element in (IEnumerable)proVal)
                                {
                                    XmlElement elementNode = xmlDoc.CreateElement("object");
                                    if (listElementType != element.GetType())
                                    {
                                        XmlHelper.SetPropertyValue(elementNode, "type", TypeHelper.GetTypeRegularName(element.GetType()), XmlPropertyPosition.Attribute);
                                    }
                                    listNode.AppendChild(elementNode);
                                    SpringFox.DoXmlObject(element, xmlDoc, elementNode);
                                }
                            }
                            #endregion
                        }
                        else
                        {
                            #region 非集合、非数组
                            bool isCollection = typeof(IEnumerable).IsAssignableFrom(pro.PropertyType);
                            bool isAry        = pro.PropertyType.IsSubclassOf(typeof(Array));

                            if ((!isCollection) && (!isAry))
                            {
                                XmlElement proNode = xmlDoc.CreateElement("property");
                                SpringFox.SetPropertyValue(proNode, "name", pro.Name, XmlPropertyPosition.Attribute);
                                curNode.AppendChild(proNode);

                                XmlElement subProNode = xmlDoc.CreateElement("object");
                                SpringFox.SetPropertyValue(subProNode, "type", TypeHelper.GetTypeRegularName(proVal.GetType()), XmlPropertyPosition.Attribute);
                                proNode.AppendChild(subProNode);

                                SpringFox.DoXmlObject(proVal, xmlDoc, subProNode);
                            }
                            #endregion
                        }
                    }
                }
                #endregion
            }
            catch (Exception ee)
            {
                string msg = string.Format("XmlObject {0} Error !", obj.GetType().ToString());
                throw new Exception(msg, ee);
            }
        }