阿牛 - 专注.NET开发

如果梦想与实现之间有一道不可逾越的鸿沟,那么“执行力”就是跨越这道鸿沟的桥梁。

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

源代码下载:DataOnlyDataTable.rar

 

public class Entity

    {

        [XmlElement]

        public int Id { get; set; }

        [XmlElement]

        public String Name { get; set; }

        [XmlElement(Type=typeof(DateTime?), IsNullable=true)]

        public DateTime? Age { get; set; }

 

        [XmlElement(DataType="date", IsNullable=false)]

        public DateTime CreateOn { get; set; }

    }

 

    [XmlRoot]

    public class MasterX

    {

        [XmlElement("Row")]

        public List<Entity> Entites { get; set; }

    }  

 

    class Program

    {

        static void Main(string[] args)

        {         

            DataSet ds = new DataSet();

            ds.ReadXmlSchema("Obj.xsd");

            ds.ReadXml("Obj.xml");

 

            Console.Write(ds.GetXml());

            Console.WriteLine();

 

            DataSet ds2 = DataSetUtil.RemoveTime(ds);

            Console.Write(ds2.GetXml());

            Console.WriteLine();

 

            DataSet ds3 = DataSetUtil.RemoveTime(ds, new String[]{"CreateOn"});

            Console.Write(ds3.GetXml());

 

            Console.WriteLine();           

            Console.Write(ds.RemoveTime().GetXml());

 

            Console.Read();

        }

    }

 

    public static class DataSetExtension

    {

        public static DataSet RemoveTime(this DataSet sourceDataSet)

        {

            return DataSetUtil.RemoveTime(sourceDataSet);

        }

 

        public static DataSet RemoveTime(this DataSet sourceDataSet, string[] dateTimeColumns)

        {

            return DataSetUtil.RemoveTime(sourceDataSet);

        }

    }

 

    public sealed class DataSetUtil

    {

        /// <summary>

        /// Remove time from source DataSet and return a new DataSet

        /// </summary>

        /// <param name="sourceDataSet"></param>

        /// <returns></returns>

        public static DataSet RemoveTime(DataSet sourceDataSet)

        {

            return RemoveTime(sourceDataSet, null);

        }

 

        /// <summary>

        /// Remove time from specified columns in source DataSet and return a new DataSet

        /// </summary>

        /// <param name="sourceDataSet"></param>

        /// <returns></returns>

        public static DataSet RemoveTime(DataSet sourceDataSet, string[] dateTimeColumns)

        {

            if (sourceDataSet == null)

            {

                throw new ArgumentNullException("sourceDataSet");

            }

            if (sourceDataSet.Tables.Count == 0)

            {

                throw new ArgumentNullException("Source DataSet contains no DataTable.");

            }

            DataSet result = sourceDataSet.Clone();

 

            //Remove all old columns from data table

            while (result.Tables[0].Columns.Count > 0)

            {

                result.Tables[0].Columns.RemoveAt(0);

            }

 

            for (int i = 0; i < sourceDataSet.Tables[0].Columns.Count; i++)

            {

                DataColumn col = sourceDataSet.Tables[0].Columns[i];

                if (CheckIfRemoveTime(col, dateTimeColumns))

                {

                        result.Tables[0].Columns.Add(col.ColumnName, typeof(String));                                          

                }

                else

                {

                    result.Tables[0].Columns.Add(col.ColumnName, col.DataType, col.Expression);

                }

            }

            for (int i = 0; i < sourceDataSet.Tables[0].Rows.Count; i++)

            {

                DataRow row = result.Tables[0].NewRow();

                for (int k = 0; k < sourceDataSet.Tables[0].Columns.Count; k++)

                {

                    if (sourceDataSet.Tables[0].Columns[k].DataType == typeof(DateTime))

                    {

                        row[k] = ((DateTime)sourceDataSet.Tables[0].Rows[i][k]).ToString("yyyy-MM-dd");

                    }

                    else

                    {

                        row[k] = sourceDataSet.Tables[0].Rows[i][k];

                    }

 

                }

                result.Tables[0].Rows.Add(row);

 

            }

 

            return result;

        }

 

        private static bool CheckIfRemoveTime(DataColumn column, string[] dateTimeColumns)

        {

            if (column.DataType != typeof(DateTime))

            {

                return false;

            } 

            if (dateTimeColumns == null)

            {

                return true;

            }

 

            return dateTimeColumns.Contains(column.ColumnName);           

        }

    }

posted on 2009-07-09 23:04  阿牛-专注金融行业开发  阅读(338)  评论(0编辑  收藏  举报