C#递归生成XML文件,递归序列化对象

        private static XmlElement CreateXmlTree(object obj, XmlDocument xDoc, XmlElement xElem)//
        {
            if (obj == null)
                return null;


            try
            {
                #region try...
                var propertyInfos = obj.GetType().GetProperties();
                XmlElement Node = xDoc.CreateElement(obj.GetType().Name);
                foreach (PropertyInfo propertyInfo in propertyInfos)
                {
                    var attributeArr = propertyInfo.GetCustomAttributes(true);
                    if (attributeArr != null)
                    {
                        #region case 1
                        if (ExistAttribute(attributeArr, "UIHXmlArrayAttributeAttribute") != null)
                        {
                            UIHXmlArrayAttributeAttribute attributeName = (UIHXmlArrayAttributeAttribute)ExistAttribute(attributeArr, "UIHXmlArrayAttributeAttribute");
                            string elementName = "";
                            if (attributeName.ElementName != null)
                            {
                                elementName = attributeName.ElementName;
                            }
                            else
                            {
                                elementName = propertyInfo.Name;
                            }


                            XmlElement xe = xDoc.CreateElement(elementName);
                            if (propertyInfo.PropertyType.IsGenericType)
                            {
                                object propertyValue = propertyInfo.GetValue(obj, null);
                                Type typeOfPropertyValue = propertyValue.GetType();
                                int numberOfListItem = Convert.ToInt32(typeOfPropertyValue.GetProperty("Count").GetValue(propertyValue, null));
                                for (int i = 0; i < numberOfListItem; i++)
                                {
                                    object listItem = typeOfPropertyValue.GetProperty("Item").GetValue(propertyValue, new object[] { i });
                                    xe = CreateXmlTree(listItem, xDoc, xe);
                                    Node.AppendChild(xe); 
                                }
                            }
                            else if (propertyInfo.PropertyType.IsClass)
                            {
                                object refObject = propertyInfo.GetValue(obj, null);
                                if (refObject != null)
                                {
                                    Type objType = refObject.GetType();
                                    xe = CreateXmlTree(refObject, xDoc, xe);
                                    Node.AppendChild(xe);
                                }
                            }
                        }
                        #endregion
                        #region case 2
                        if (ExistAttribute(attributeArr, "UIHXmlAttributeAttribute") != null)
                        {
                            UIHXmlAttributeAttribute attributeName = (UIHXmlAttributeAttribute)ExistAttribute(attributeArr, "UIHXmlAttributeAttribute");


                            #region get element name
                            string elementName = "";
                            if (attributeName.AttributeName != null)
                            {
                                elementName = attributeName.AttributeName;
                            }
                            else
                            {
                                elementName = propertyInfo.Name;
                            }
                            #endregion


                            if (propertyInfo.GetValue(obj, null) != null)
                            {
                                #region get value of property
                                string propertyValue = "";
                                if (propertyInfo.PropertyType == typeof(TimeSpan))
                                {
                                    DateTime start = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
                                    TimeSpan timeSpan = (TimeSpan)propertyInfo.GetValue(obj, null);
                                    DateTime ttt = start.Add(timeSpan);
                                    propertyValue = ttt.ToString("yyyy/MM/dd-HH:mm:ss:fff");
                                }
                                else
                                {
                                    propertyValue = propertyInfo.GetValue(obj, null).ToString();
                                }
                                #endregion


                                if (attributeName.DefaultValue == null)
                                {
                                    XmlAttribute xe = xDoc.CreateAttribute(elementName);
                                    xe.Value = propertyValue;
                                    Node.Attributes.Append(xe);
                                }
                                else if (attributeName.DefaultValue != propertyValue)
                                {
                                    XmlAttribute xe = xDoc.CreateAttribute(elementName);
                                    xe.Value = propertyValue;
                                    Node.Attributes.Append(xe);
                                }
                            }
                        }
                        #endregion
                    }
                }


                xElem.AppendChild(Node);
                return xElem;
                #endregion
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
            return null;
        }

        private static object ExistAttribute(object[] Attributes, string AttributeName)//找到特定的特性
        {
            for (int i = 0; i < Attributes.Length; ++i)
            {
                if (Attributes[i].GetType().Name == AttributeName)
                {
                    return Attributes[i];
                }
            }
            return null;

        }


    [Serializable]
    [AttributeUsage(AttributeTargets.Property | AttributeTargets.Enum | AttributeTargets.Parameter |
        AttributeTargets.Class | AttributeTargets.ReturnValue,AllowMultiple = false,Inherited = true)]
    class UIHXmlAttributeAttribute:Attribute
    {
        public UIHXmlAttributeAttribute()                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
        {


        }
        public UIHXmlAttributeAttribute(string elemName, string defaultValue)
        {
            AttributeName = elemName;
            DefaultValue = defaultValue;
        }
        public string AttributeName { set; get; }


        public string DefaultValue { set; get; }
    }


[Serializable]
    [AttributeUsage(AttributeTargets.Property | AttributeTargets.ReturnValue | AttributeTargets.Class ,AllowMultiple = false)]
    class UIHXmlArrayAttributeAttribute : Attribute
    {
        public UIHXmlArrayAttributeAttribute()
        {


        }
        public UIHXmlArrayAttributeAttribute(string elemName,Type typeOfItem)
        {
            ElementName = elemName;
            Type = typeOfItem;
        }


        public UIHXmlArrayAttributeAttribute(Type typeOfItem)
        {
            Type = typeOfItem;
        }


        public string ElementName {set;get;}


        public Type Type { set; get; }


    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值