把对象输出XML的类

 

using System;

using System.Collections.Generic;

using System.Text;

using System.Xml;

using System.Collections;

namespace Exports

{

 

    public class ExportXml : IDisposable

    {

        public ExportXml(System.IO.TextWriter write, object data)

        {

            mTextWrite = write;

            mWriter = new System.Xml.XmlTextWriter(write);

            mDate = data;

 

        }

        private System.IO.TextWriter mTextWrite;

        private XmlWriter mWriter;

        public XmlWriter Writer

        {

            get

            {

                return mWriter;

            }

 

        }

        private object mDate;

        public Object Data

        {

            get

            {

                return mDate;

            }

        }

        public void Execute()

        {

            Writer.WriteStartElement(Data.GetType().Name);

            object value;

            foreach (NClay.PropertyHandler handler in GetHandler(Data.GetType()))

            {

                Writer.WriteStartElement(handler.Property.Name);

                value = handler.Get(Data);

                if (value == null)

                    continue;

                if (value is ValueType)

                {

 

                    Writer.WriteString(value.ToString());

                }

                else if (value is string)

                {

                    Writer.WriteString(value.ToString());

                }

                else if (value is IEnumerable)

                {

                    foreach (object item in (IEnumerable)value)

                    {

                        if (item is ValueType || item is string)

                        {

                            Writer.WriteStartElement("Value");

                            Writer.WriteString(item.ToString());

                            Writer.WriteEndElement();

                        }

                        else

                        {

 

                            using (ExportXml export = new ExportXml(mTextWrite, value))

                            {

                                export.Execute();

                            }

                        }

                    }

                }

                Writer.WriteEndElement();

            }

            Writer.WriteEndElement();

            Writer.Flush();

        }

        static IDictionary<Type, List<NClay.PropertyHandler>> mPropertyHandlers

            = new Dictionary<Type, List<NClay.PropertyHandler>>();

        static IList<NClay.PropertyHandler> GetHandler(Type type)

        {

            List<NClay.PropertyHandler> handlers;

            if (mPropertyHandlers.ContainsKey(type))

            {

                handlers = mPropertyHandlers[type];

            }

            else

            {

                lock (typeof(ExportXml))

                {

                    if (mPropertyHandlers.ContainsKey(type))

                    {

                        handlers = mPropertyHandlers[type];

                    }

                    else

                    {

                        handlers = CreateHandlers(type);

                        mPropertyHandlers.Add(type, handlers);

                    }

                }

            }

            return handlers;

        }

        static List<NClay.PropertyHandler> CreateHandlers(Type type)

        {

            List<NClay.PropertyHandler> handlers = new List<NClay.PropertyHandler>();

            foreach (System.Reflection.PropertyInfo item in

                type.GetProperties(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public))

            {

                handlers.Add(new NClay.PropertyHandler(item));

            }

            return handlers;

        }

 

 

        #region IDisposable 成员

 

        public void Dispose()

        {

            Writer.Flush();

            mWriter = null;

            mTextWrite = null;

        }

 

        #endregion

    }

}

 

posted on 2007-10-16 17:31  henry  阅读(393)  评论(0编辑  收藏  举报

导航