Ejemplo n.º 1
0
        public static string XmlSerializeObject(object target)
        {
            string      typeName = TypeHelper.GetClassSimpleName(target.GetType());
            XmlDocument xmlDoc   = new XmlDocument();
            XmlElement  objNode  = xmlDoc.CreateElement(typeName);

            foreach (PropertyInfo pro in target.GetType().GetProperties())
            {
                if ((!pro.CanRead))
                {
                    continue;
                }

                SimpleXmlConverter.DoXmlSerializeObject(objNode, pro, pro.GetValue(target, null));
            }

            return(objNode.OuterXml);
        }
Ejemplo n.º 2
0
        private static void DoXmlSerializeObject(XmlNode curNode, PropertyInfo pro, object val)
        {
            bool isGenericList = pro.PropertyType.IsGenericType && (pro.PropertyType.GetGenericTypeDefinition() == typeof(IList <>));

            if (isGenericList)
            {
                string  listName = pro.Name;
                XmlNode listNode = curNode.OwnerDocument.CreateElement(listName);
                curNode.AppendChild(listNode);

                Type listElementType = pro.PropertyType.GetGenericArguments()[0];
                if (TypeHelper.IsSimpleType(listElementType))
                {
                    foreach (object element in (IEnumerable)val)
                    {
                        XmlHelper.SetPropertyValue(listNode, "value", element.ToString(), XmlPropertyPosition.ChildNode, false);
                    }
                }
                else
                {
                    foreach (object element in (IEnumerable)val)
                    {
                        string  newObjName = TypeHelper.GetClassSimpleName(listElementType);
                        XmlNode newObjNode = curNode.OwnerDocument.CreateElement(newObjName);
                        listNode.AppendChild(newObjNode);

                        PropertyInfo[] childPros = listElementType.GetProperties();
                        foreach (PropertyInfo childPro in childPros)
                        {
                            if ((!childPro.CanRead))
                            {
                                continue;
                            }

                            object childVal = childPro.GetValue(element, null);
                            SimpleXmlConverter.DoXmlSerializeObject(newObjNode, childPro, childVal);
                        }
                    }
                }
            }
            else if (TypeHelper.IsSimpleType(pro.PropertyType))
            {
                string valStr = (val == null ? "" : val.ToString());
                XmlHelper.SetPropertyValue(curNode, pro.Name, valStr);
            }
            else
            {
                string  newObjName = TypeHelper.GetClassSimpleName(pro.PropertyType);
                XmlNode newObjNode = curNode.OwnerDocument.CreateElement(newObjName);
                curNode.AppendChild(newObjNode);

                PropertyInfo[] childPros = pro.PropertyType.GetProperties();
                foreach (PropertyInfo childPro in childPros)
                {
                    if ((!childPro.CanRead))
                    {
                        continue;
                    }

                    SimpleXmlConverter.DoXmlSerializeObject(newObjNode, childPro, childPro.GetValue(val, null));
                }
            }
        }