框架中的 XML读取!
当然在ADO.NET 中,XML和DataSet是可以相互“转换"的!
但是,在框架里面我们做ORM映射的时候,往往希望一特定的XML配置文件来获取对象;
我们在读取具体格式xml数据的时候,就不能随心所欲了;
但是ADO.NET 的强大的XML处理能力,使得读取起来仍然方便,下面这个实例给了我们一些简单的解决办法:
显示实体类的配置文件:
<?xml version="1.0" encoding="utf-8" ?>
<Entity>
<EntityTypeName>RX_Projects</EntityTypeName>
<MemberDeclare>
<Member Name="RX_Projects" Type="Main" />
</MemberDeclare>
<MemberDefinition>
<EntityMember Name="RX_Projects">
<Keys>
<Key Name="Id" />
</Keys>
<Fields>
<Field Name="Id" DataType="System.Int32" Length="" />
<Field Name="iOrgId" DataType="System.Int32" Length="" />
<Field Name="vCode" DataType="System.String" Length="50" />
<Field Name="vName" DataType="System.String" Length="200" />
<Field Name="vSimpName" DataType="System.String" Length="100" />
<Field Name="iComId" DataType="System.Int32" Length="" />
<Field Name="vPrincipal" DataType="System.String" Length="20" />
<Field Name="vPhone" DataType="System.String" Length="30" />
<Field Name="vMemo" DataType="System.String" Length="600" />
<Field Name="iState" DataType="System.Int32" Length="" />
<Field Name="iTransId" DataType="System.Int32" Length="" />
<Field Name="sTranDate" DataType="System.DateTime" Length="" />
<Field Name="iCheckId" DataType="System.Int32" Length="" />
<Field Name="sCheckDate" DataType="System.DateTime" Length="" />
<Field Name="iFinishId" DataType="System.Int32" Length="" />
<Field Name="sFinishDate" DataType="System.DateTime" Length="" />
<Field Name="iCancelId" DataType="System.Int32" Length="" />
<Field Name="sCancelDate" DataType="System.DateTime" Length="" />
<Field Name="iIsDelete" DataType="System.Int32" Length="" />
</Fields>
</EntityMember>
</MemberDefinition>
<RelationRX_Projectss />
<SqlCollections>
<Sql Name="InsertRX_Projects" AutoGenerate="True" TableName="RX_Projects" SqlOperationType="Insert" />
<Sql Name="UpdateRX_Projects" AutoGenerate="True" TableName="RX_Projects" SqlOperationType="Update" />
<Sql Name="SelectRX_Projects" AutoGenerate="True" TableName="RX_Projects" SqlOperationType="SimpleSelect" />
<Sql Name="SelectRX_ProjectsByKey" AutoGenerate="True" TableName="RX_Projects" SqlOperationType="SelectByKey" />
<Sql Name="DeleteRX_ProjectsByKey" AutoGenerate="True" TableName="RX_Projects" SqlOperationType="DeleteByKey" />
<!--得到未删除的已审核的项目-->
<Sql Name="GetProjectList" TableName="RX_Projects" AutoGenerate="False">
<String>
<![CDATA[
select * from RX_Projects
where iOrgId = @iOrgId
and iState <>1
and iState <>4
and iIsDelete = 0
]]>
</String>
<Param Name="@iOrgId" FieldName="iOrgId"></Param>
</Sql>
</SqlCollections>
</Entity>
<Entity>
<EntityTypeName>RX_Projects</EntityTypeName>
<MemberDeclare>
<Member Name="RX_Projects" Type="Main" />
</MemberDeclare>
<MemberDefinition>
<EntityMember Name="RX_Projects">
<Keys>
<Key Name="Id" />
</Keys>
<Fields>
<Field Name="Id" DataType="System.Int32" Length="" />
<Field Name="iOrgId" DataType="System.Int32" Length="" />
<Field Name="vCode" DataType="System.String" Length="50" />
<Field Name="vName" DataType="System.String" Length="200" />
<Field Name="vSimpName" DataType="System.String" Length="100" />
<Field Name="iComId" DataType="System.Int32" Length="" />
<Field Name="vPrincipal" DataType="System.String" Length="20" />
<Field Name="vPhone" DataType="System.String" Length="30" />
<Field Name="vMemo" DataType="System.String" Length="600" />
<Field Name="iState" DataType="System.Int32" Length="" />
<Field Name="iTransId" DataType="System.Int32" Length="" />
<Field Name="sTranDate" DataType="System.DateTime" Length="" />
<Field Name="iCheckId" DataType="System.Int32" Length="" />
<Field Name="sCheckDate" DataType="System.DateTime" Length="" />
<Field Name="iFinishId" DataType="System.Int32" Length="" />
<Field Name="sFinishDate" DataType="System.DateTime" Length="" />
<Field Name="iCancelId" DataType="System.Int32" Length="" />
<Field Name="sCancelDate" DataType="System.DateTime" Length="" />
<Field Name="iIsDelete" DataType="System.Int32" Length="" />
</Fields>
</EntityMember>
</MemberDefinition>
<RelationRX_Projectss />
<SqlCollections>
<Sql Name="InsertRX_Projects" AutoGenerate="True" TableName="RX_Projects" SqlOperationType="Insert" />
<Sql Name="UpdateRX_Projects" AutoGenerate="True" TableName="RX_Projects" SqlOperationType="Update" />
<Sql Name="SelectRX_Projects" AutoGenerate="True" TableName="RX_Projects" SqlOperationType="SimpleSelect" />
<Sql Name="SelectRX_ProjectsByKey" AutoGenerate="True" TableName="RX_Projects" SqlOperationType="SelectByKey" />
<Sql Name="DeleteRX_ProjectsByKey" AutoGenerate="True" TableName="RX_Projects" SqlOperationType="DeleteByKey" />
<!--得到未删除的已审核的项目-->
<Sql Name="GetProjectList" TableName="RX_Projects" AutoGenerate="False">
<String>
<![CDATA[
select * from RX_Projects
where iOrgId = @iOrgId
and iState <>1
and iState <>4
and iIsDelete = 0
]]>
</String>
<Param Name="@iOrgId" FieldName="iOrgId"></Param>
</Sql>
</SqlCollections>
</Entity>
其中,EntityData为一空的实体数据集,Type为Dataset;root 为XML根节点
protected void BuildFields(XmlNode root, EntityData entity)
{
XmlNodeList list = root.SelectNodes("MemberDefinition/EntityMember");
if (((this.menbers != null) && (this.menbers.Length > 0)) && (list != null))
{
for (int i = 0; i < list.Count; i++)
{
string name = list[i].Attributes["Name"].Value;
if (entity.Tables.Contains(name))
{
DataTable table = entity.Tables[name];
XmlNodeList list2 = list[i].SelectNodes("Fields/Field");
for (int j = 0; j < list2.Count; j++)
{
DataColumn column = new DataColumn(list2[j].Attributes["Name"].Value, Type.GetType(list2[j].Attributes["DataType"].Value));
if (list2[j].Attributes["DefaultValue"] != null)
{
column.DefaultValue = list2[j].Attributes["DefaultValue"].Value;
}
table.Columns.Add(column);
}
XmlNodeList list3 = list[i].SelectNodes("Keys/Key");
if ((list3 != null) && (list3.Count > 0))
{
DataColumn[] columnArray = new DataColumn[list3.Count];
for (int k = 0; k < list3.Count; k++)
{
columnArray[k] = table.Columns[list3[k].Attributes["Name"].Value];
if (columnArray[k].DefaultValue.Equals(DBNull.Value))
{
columnArray[k].DefaultValue = "0";
}
}
table.PrimaryKey = columnArray;
}
}
}
}
}
{
XmlNodeList list = root.SelectNodes("MemberDefinition/EntityMember");
if (((this.menbers != null) && (this.menbers.Length > 0)) && (list != null))
{
for (int i = 0; i < list.Count; i++)
{
string name = list[i].Attributes["Name"].Value;
if (entity.Tables.Contains(name))
{
DataTable table = entity.Tables[name];
XmlNodeList list2 = list[i].SelectNodes("Fields/Field");
for (int j = 0; j < list2.Count; j++)
{
DataColumn column = new DataColumn(list2[j].Attributes["Name"].Value, Type.GetType(list2[j].Attributes["DataType"].Value));
if (list2[j].Attributes["DefaultValue"] != null)
{
column.DefaultValue = list2[j].Attributes["DefaultValue"].Value;
}
table.Columns.Add(column);
}
XmlNodeList list3 = list[i].SelectNodes("Keys/Key");
if ((list3 != null) && (list3.Count > 0))
{
DataColumn[] columnArray = new DataColumn[list3.Count];
for (int k = 0; k < list3.Count; k++)
{
columnArray[k] = table.Columns[list3[k].Attributes["Name"].Value];
if (columnArray[k].DefaultValue.Equals(DBNull.Value))
{
columnArray[k].DefaultValue = "0";
}
}
table.PrimaryKey = columnArray;
}
}
}
}
}