源代码下载: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
{
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);
}
}