using System;
using System.Xml ;
using System.Xml.Serialization ;
using System.Xml.XPath ;
using System.Reflection;
namespace WebApplication2.Xml
{
/// <summary>
/// Procedure 的摘要说明。
/// </summary>
public class Procedure
{
public Procedure()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
private Command[] _Commands;
[XmlArray("Commands")]
public Command[] Commands
{
get
{
return this._Commands;}
set
{
this._Commands=value;
}
}
public static object Instance(System.Web.HttpContext context)
{
System.Xml.XmlDocument doc=new System.Xml.XmlDocument();
doc.Load(context.Server.MapPath("Xml/Procedure.xml"));
System.Xml.XmlNodeList nodeList=doc.GetElementsByTagName("Procedure");
if(nodeList.Count!=1)
throw new Exception("配置参数出错了!!");
System.Xml.XmlNode section=nodeList.Item(0);
XPathNavigator nav = section.CreateNavigator();
string typename = (string) nav.Evaluate("string(@type)");
Type t = Type.GetType(typename);
XmlSerializer ser = new XmlSerializer(t);
XmlNodeReader xnr=new XmlNodeReader(section);
object obj= ser.Deserialize(xnr);
return obj;
}
}
public class Pm
{
/*<Pm ParameterName="@ID" Direction="Output" SqlDbType="Int" Size="4"></Pm>*/
public Pm()
{
}
private string _ParameterName;
[XmlAttribute("ParameterName")]
public string ParameterName
{
get
{
return this._ParameterName;}
set
{
this._ParameterName=value;
}
}
private System.Data.ParameterDirection _Direction;
[XmlAttribute("Direction")]
public System.Data.ParameterDirection Direction
{
get
{
return this._Direction;}
set
{
this._Direction=value;
}
}
private System.Data.SqlDbType _SqlType;
[XmlAttribute("SqlDbType")]
public System.Data.SqlDbType SqlDbType
{
get
{
return this._SqlType;}
set
{
this._SqlType=value;
}
}
private int _KeyNo;
[XmlAttribute("KeyNo")]
public int KeyNo
{
get
{
return this._KeyNo;}
set
{
this._KeyNo=value;
}
}
private int _Size;
[XmlAttribute("Size")]
public int Size
{
get
{
return this._Size;}
set
{
this._Size=value;
}
}
private readonly object HandlerLock = new object();
private ConstructorInfo constructor = null;
public object Instance()
{
if(constructor == null)
{
lock(HandlerLock)
{
if(constructor == null)
{
System.Type t = System.Type.GetType("WebApplication2.Pm,WebApplication2");
constructor = t.GetConstructor(new Type[0]);
}
}
}
return constructor.Invoke(null);
}
public System.Data.SqlClient.SqlParameter toSqlParameter()
{
System.Data.SqlClient.SqlParameter pa=new System.Data.SqlClient.SqlParameter();
pa.ParameterName=this.ParameterName;
pa.Direction=this.Direction ;
pa.Size=this.Size;
pa.SqlDbType=this.SqlDbType;
return pa;
}
}
public class Command
{
private int _ArgsLen;
[XmlAttribute("ArgsLen")]
public int ArgsLen
{
get
{
return this._ArgsLen;
}
set
{
this._ArgsLen=value;
}
}
private string _CommandText;
[XmlAttribute("CommandText")]
public string CommandText
{
get
{
return this._CommandText;}
set
{
this._CommandText=value;
}
}
private Pm[] _Parameters;
[XmlArray("Parameters")]
public Pm[] Parameters
{
get
{
return this._Parameters;}
set
{
this._Parameters=value;
}
}
public Command()
{
}
private readonly object HandlerLock = new object();
private ConstructorInfo constructor = null;
public object Instance()
{
if(constructor == null)
{
lock(HandlerLock)
{
if(constructor == null)
{
System.Type t = System.Type.GetType("WebApplication2.Command,WebApplication2");
constructor = t.GetConstructor(new Type[0]);
}
}
}
return constructor.Invoke(null);
}
// private readonly object argLock = new object();
// public ConstructorInfo argConstructor=null;
// public object ArgInstance()
// {
// if(argConstructor == null)
// {
// lock(argLock)
// {
// if(argConstructor == null)
// {
// System.Type t = System.Type.GetType(this.Args);
// argConstructor = t.GetConstructor(new Type[0]);
// }
// }
// }
// return constructor.Invoke(null);
// }
public System.Data.SqlClient.SqlCommand toSqlCommand()
{
System.Data.SqlClient.SqlCommand cmd=new System.Data.SqlClient.SqlCommand();
cmd.CommandText=this.CommandText;
cmd.CommandType=System.Data.CommandType.StoredProcedure;
foreach(Pm pm in this.Parameters)
{
cmd.Parameters.Add(pm.toSqlParameter ());
}
return cmd;
}
//<Command CommandText="tAdd" CommandType="StoredProcedure">
}
}
//////////////////////////xml访如下
<?xml version="1.0" encoding="utf-8" ?>
<Procedure type="WebApplication2.Xml.Procedure,WebApplication2">
<Commands>
<Command CommandText="tAdd" ArgsLen="2">
<Parameters>
<Pm KeyNo="1" ParameterName="@ID" Direction="Output" SqlDbType="Int" Size="4"></Pm>
<Pm KeyNo="2" ParameterName="@PC" Direction="Input" SqlDbType="VarChar" Size="50"></Pm>
</Parameters>
</Command>
<Command CommandText="tUpDate" ArgsLen="2">
<Parameters>
<Pm KeyNo="1" ParameterName="@ID" Direction="Input" SqlDbType="Int" Size="4"></Pm>
<Pm KeyNo="2" ParameterName="@PC" Direction="Input" SqlDbType="VarChar" Size="50"></Pm>
</Parameters>
</Command>
</Commands>
</Procedure>
调用测试代码如下
WebApplication2.Xml.Procedure pro=Xml.Procedure.Instance(this.Context) as Xml.Procedure;
if(pro==null)
Response.Write("no ,no Procedure found<BR>");
else
{
Response.Write(pro.Commands.Length);
Response.Write("<BR>");
foreach(WebApplication2.Xml.Command cmd in pro.Commands)
{
Response.Write("  "+cmd.CommandText+"<BR>");
foreach(WebApplication2.Xml.Pm pm in cmd.Parameters)
{
Response.Write(pm.toSqlParameter ().ParameterName+"<BR>");
}
}
}