level 1
凌晨炫火
楼主
#region 导出数据XML
protected void ExportXmlEntity(object sender, DirectEventArgs e)
{
// 根节点添加到文档中
XmlDocument doc = new XmlDocument();
XmlElement Root = doc.CreateElement("data");//主内容
doc.AppendChild(Root);
XmlElement Child = doc.CreateElement("ROWSET");
XmlAttribute attr = doc.CreateAttribute("tableName");
attr.Value = "jm_pre_cont";
Child.Attributes.Append(attr);
Root.AppendChild(Child);
// 获取表结构
string tableName = "JM_PRE_CONT";
string sql = "SELECT NAME AS FIELD_NAME,LENGTH AS FIELD_LENGTH FROM syscolumns WHERE id=object_id(*" + tableName + "*)";
List<FIELD_PRO> fieldList = services.ModelHelper .GetEntityListBySql<FIELD_PRO> (sql);
// 获取表数据
List<JM_PRE_CONT> actions = services.ModelHelper.GetEntityList<JM_PRE_CONT>();
DataTable actionDT = ListToDataTable(actions);
for (int i = 0; i < actions.Count;i++ )
{
XmlElement Child1 = doc.CreateElement("ROW");
XmlAttribute attr1 = doc.CreateAttribute("action");
attr1.Value = "insert";
Child1.Attributes.Append(attr1);
Child.AppendChild(Child1);
for (int j = 0; j < fieldList.Count;j++ )
{
XmlElement field = doc.CreateElement("Field");
XmlAttribute fieldName = doc.CreateAttribute("fieldName");//列名
XmlAttribute fieldLength = doc.CreateAttribute("maxLength");//列的长度
fieldName.Value = fieldList[j].FIELD_NAME ;
fieldLength.Value = fieldList[j].FIELD_LENGTH ;
field.InnerText = actionDT.Rows[i][j].ToString();
field.Attributes.Append(fieldName);
field.Attributes.Append(fieldLength);
Child1.AppendChild(field);
}
}
doc.Save("F://1.xml");//保存这个xml 网页或exe 都可以
}
#endregion
public static DataTable ListToDataTable<T>(List<T> entitys)
{
//检查实体集合不能为空
if (entitys == null || entitys.Count < 1)
{
return new DataTable();
}
//取出第一个实体的所有Propertie
Type entityType = entitys[0].GetType();
PropertyInfo[] entityProperties = entityType.GetProperties();
//生成DataTable的structure
//生产代码中,应将生成的DataTable结构Cache起来
DataTable dt = new DataTable("dt");
for (int i = 0; i < entityProperties.Length; i++)
{
//dt.Columns.Add(entityProperties[i].Name, entityProperties[i].PropertyType);
dt.Columns.Add(entityProperties[i].Name);
}
//将所有entity添加到DataTable中
foreach (object entity in entitys)
{
//检查所有的的实体都为同一类型
if (entity.GetType() != entityType)
{
throw new Exception("要转换的集合元素类型不一致");
}
object[] entityValues = new object[entityProperties.Length];
for (int i = 0; i < entityProperties.Length; i++)
{
entityValues[i] = entityProperties[i].GetValue(entity, null);
}
dt.Rows.Add(entityValues);
}
return dt;
}
}
2014年07月25日 01点07分
1
protected void ExportXmlEntity(object sender, DirectEventArgs e)
{
// 根节点添加到文档中
XmlDocument doc = new XmlDocument();
XmlElement Root = doc.CreateElement("data");//主内容
doc.AppendChild(Root);
XmlElement Child = doc.CreateElement("ROWSET");
XmlAttribute attr = doc.CreateAttribute("tableName");
attr.Value = "jm_pre_cont";
Child.Attributes.Append(attr);
Root.AppendChild(Child);
// 获取表结构
string tableName = "JM_PRE_CONT";
string sql = "SELECT NAME AS FIELD_NAME,LENGTH AS FIELD_LENGTH FROM syscolumns WHERE id=object_id(*" + tableName + "*)";
List<FIELD_PRO> fieldList = services.ModelHelper .GetEntityListBySql<FIELD_PRO> (sql);
// 获取表数据
List<JM_PRE_CONT> actions = services.ModelHelper.GetEntityList<JM_PRE_CONT>();
DataTable actionDT = ListToDataTable(actions);
for (int i = 0; i < actions.Count;i++ )
{
XmlElement Child1 = doc.CreateElement("ROW");
XmlAttribute attr1 = doc.CreateAttribute("action");
attr1.Value = "insert";
Child1.Attributes.Append(attr1);
Child.AppendChild(Child1);
for (int j = 0; j < fieldList.Count;j++ )
{
XmlElement field = doc.CreateElement("Field");
XmlAttribute fieldName = doc.CreateAttribute("fieldName");//列名
XmlAttribute fieldLength = doc.CreateAttribute("maxLength");//列的长度
fieldName.Value = fieldList[j].FIELD_NAME ;
fieldLength.Value = fieldList[j].FIELD_LENGTH ;
field.InnerText = actionDT.Rows[i][j].ToString();
field.Attributes.Append(fieldName);
field.Attributes.Append(fieldLength);
Child1.AppendChild(field);
}
}
doc.Save("F://1.xml");//保存这个xml 网页或exe 都可以
}
#endregion
public static DataTable ListToDataTable<T>(List<T> entitys)
{
//检查实体集合不能为空
if (entitys == null || entitys.Count < 1)
{
return new DataTable();
}
//取出第一个实体的所有Propertie
Type entityType = entitys[0].GetType();
PropertyInfo[] entityProperties = entityType.GetProperties();
//生成DataTable的structure
//生产代码中,应将生成的DataTable结构Cache起来
DataTable dt = new DataTable("dt");
for (int i = 0; i < entityProperties.Length; i++)
{
//dt.Columns.Add(entityProperties[i].Name, entityProperties[i].PropertyType);
dt.Columns.Add(entityProperties[i].Name);
}
//将所有entity添加到DataTable中
foreach (object entity in entitys)
{
//检查所有的的实体都为同一类型
if (entity.GetType() != entityType)
{
throw new Exception("要转换的集合元素类型不一致");
}
object[] entityValues = new object[entityProperties.Length];
for (int i = 0; i < entityProperties.Length; i++)
{
entityValues[i] = entityProperties[i].GetValue(entity, null);
}
dt.Rows.Add(entityValues);
}
return dt;
}
}

