您的位置:首页 > 编程语言 > C#

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

2016-06-14 20:44 330 查看
        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; }

    }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c#