110,502
社区成员
发帖
与我相关
我的任务
分享
<request>
<insert>
<fields>
<RepUser>
<RepID></RepID><!--主键-->
<OrgGrpID></OrgGrpID><!--所属部门-->
<Name></Name><!--名称-->
<Sex></Sex><!--性别-->
<LastName></LastName><!--姓-->
<FirstName></FirstName><!--名-->
<Country></Country><!--国家/地区-->
<ZipCode></ZipCode><!--邮编-->
<City></City><!--城市-->
<Street></Street ><!--街道-->
<CountryCode></CountryCode><!--国家代码-->
<Tel1></Tel1><!--电话1-->
<Tel2></Tel2><!--电话2-->
<Name></Name><!--名称-->
<Fax></Fax><!--传真-->
<New></New><!--创建日期-->
<Upd></Upd><!--更新日期-->
<E-mail1></E-mail1><!--电子邮件-->
<TMRep></TMRep><!--TM用户-->
<LanguageNo></LanguageNo><!--语言编号-->
<E-mail2></E-mail2><!--电子邮件2-->
<E-mailPriv></E-mailPriv><!--私人电子邮件-->
<Type></Type><!--类型-->
<Inactive></Inactive><!--离职-->
<SuperiorID></SuperiorID><!--上级-->
<ExtSystem></ExtSystem><!--外部系统-->
<ExtKey></ExtKey><!--外部主键-->
<HierarchyCode></HierarchyCode><!--层次结构代码-->
<ApproverRole></ApproverRole><!--审批人角色-->
<RepProperty></RepProperty><!--销售属性-->
<Position></Position><!--岗位-->
</RepUser>
</fields>
</insert>
</request>
<request>
<update>
<tables>
<table tablename="RepUser"/>
</tables>
<condition>
<cond tablename="RepUser" fieldname="RepID" op="=" value="88"/>
</condition>
<fields>
<RepUser>
<RepID></RepID><!--主键-->
<OrgGrpID></OrgGrpID><!--所属部门-->
<Name></Name><!--名称-->
<Sex></Sex><!--性别-->
<LastName></LastName><!--姓-->
<FirstName></FirstName><!--名-->
....
</RepUser>
</fields>
</update>
</request>
RepUser repUser = new RepUser();
repUser.City = "上海";
repUser.Country = "中国";
repUser.ApproverRole = "总经理";
//输出insert的XML字串
repUser.outputType = ChangeType.INSERT;
Console.WriteLine("Insert request is:");
Console.WriteLine(repUser);
//输出update的XML字串
repUser.City = "北京";
repUser.Country = "中国";
repUser.ApproverRole = "副总经理";
repUser.outputType = ChangeType.UPDATE;
Console.WriteLine("Update request is:");
Console.WriteLine(repUser);
using System.Reflection;
namespace XMLRequst
{
public class RepUser
{
private const string MAINKEY = "RepID";
private int _RepID;//主键
private string _OrgGrpID;//所属部门
private string _Name;//名称
private bool _Sex;//性别
private string _LastName;//姓
private string _FirstName;//名
private string _Country;//国家/地区
private string _ZipCode;//邮编
private string _City;//城市
private string _Street;//街道
private string _CountryCode;//国家代码
private string _Tel1;//电话1
private string _Tel2;//电话2
private string _Fax;//传真
private DateTime _New;//创建日期
private DateTime _Upd;//更新日期
private string _E_mail1;//电子邮件
private bool _TMRep;//TM用户
private string _LanguageNo;//语言编号
private string _E_mail2;//电子邮件2
private string _E_mailPriv;//私人电子邮件
private string _Type;//类型
private bool _Inactive;//离职
private int _SuperiorID;//上级
private string _ExtSystem;//外部系统
private string _ExtKey;//外部主键
private string _HierarchyCode;//层次结构代码
private string _ApproverRole;//审批人角色
private string _RepProperty;//销售属性
private string _Position;//岗位
/// <summary>
/// 输出格式,INSERT和UPDATE
/// </summary>
public ChangeType outputType;
private Dictionary<string, bool> _propertiesChangeMap;
public int RepID { get { return _RepID; } set { changeProperties("RepID" ,value); } }//主键
public string OrgGrpID { get { return _OrgGrpID; } set { changeProperties("OrgGrpID", value); } }//所属部门
public string Name { get { return _Name; } set { changeProperties("Name", value); } }//名称
public bool Sex { get { return _Sex; } set { changeProperties("Sex", value); } }//性别
public string LastName { get { return _LastName; } set { changeProperties("LastName", value); } }//姓
public string FirstName { get { return _FirstName; } set { changeProperties("FirstName", value); } }//名
public string Country { get { return _Country; } set { changeProperties("Country", value); } }//国家/地区
public string ZipCode { get { return _ZipCode; } set { changeProperties("ZipCode", value); } }//邮编
public string City { get { return _City; } set { changeProperties("City", value); } }//城市
public string Street{ get { return _Street; } set { changeProperties("Street", value); } }//街道
public string CountryCode { get { return _CountryCode; } set { changeProperties("CountryCode", value); } }//国家代码
public string Tel1 { get { return _Tel1; } set { changeProperties("Tel1", value); } }//电话1
public string Tel2 { get { return _Tel2; } set { changeProperties("Tel2", value); } }//电话2
public string Fax{ get { return _Fax; } set { changeProperties("Fax", value); } }//传真
public DateTime New { get { return _New; } set { changeProperties("New", value); } }//创建日期
public DateTime Upd { get { return _Upd; } set { changeProperties("Upd", value); } }//更新日期
public string E_mail1 { get { return _E_mail1; } set { changeProperties("E_mail1", value); } }//更新日期
public bool TMRep { get { return _TMRep; } set { changeProperties("TMRep", value); } }//电子邮件
public string LanguageNo { get { return _LanguageNo; } set { changeProperties("LanguageNo", value); } }//语言编号
public string E_mail2 { get { return _E_mail2; } set { changeProperties("E_mail2", value); } }//电子邮件2
public string E_mailPriv { get { return _E_mailPriv; } set { changeProperties("E_mailPriv", value); } }//私人电子邮件
public string Type { get { return _Type; } set { changeProperties("Type", value); } }//类型
public bool Inactive { get { return _Inactive; } set { changeProperties("Inactive", value); } }//离职
public int SuperiorID { get { return _SuperiorID; } set { changeProperties("SuperiorID", value); } }//上级
public string ExtSystem { get { return _ExtSystem; } set { changeProperties("ExtSystem", value); } }//外部系统
public string ExtKey { get { return _ExtKey; } set { changeProperties("ExtKey", value); } }//外部主键
public string HierarchyCode { get { return _HierarchyCode; } set { changeProperties("HierarchyCode", value); } }//层次结构代码
public string ApproverRole { get { return _ApproverRole; } set { changeProperties("ApproverRole", value); } }//审批人角色
public string RepProperty { get { return _RepProperty; } set { changeProperties("RepProperty", value); } }//销售属性
public string Position { get { return _Position; } set { changeProperties("Position", value); } }//岗位
public RepUser(){
_New = DateTime.Now;
_propertiesChangeMap = new Dictionary<string,bool>();
}
/// <summary>
/// 修改属性
/// </summary>
/// <param name="propertyName">属性名</param>
/// <param name="value">属性值</param>
/// <returns></returns>
private bool changeProperties(string propertyName,object value)
{
Type t = this.GetType();
FieldInfo f = t.GetField("_" + propertyName, BindingFlags.NonPublic | BindingFlags.Instance);
if (f == null)
return false;
f.SetValue(this, value);
_Upd = DateTime.Now;
//_propertiesChangeMap.Add(propertyName, true);
//if (_propertiesChangeMap.ContainsKey(propertyName))
//{
// _propertiesChangeMap.
// }
//_propertiesChangeMap.Add("Upd", true);
_propertiesChangeMap["Upd"] = true;
_propertiesChangeMap[propertyName] = true;
return true;
}
/// <summary>
/// 输出XML后或者需要修改输出方式时候先调用此函数再修改参数
/// </summary>
public void finishChange(){
_propertiesChangeMap.Clear();
}
/// <summary>
/// 输出XML格式的字串,如果其他地方需要使用原来的ToString函数,可将此函数改为新的函数,保留原ToString函数。
/// </summary>
/// <returns>类输出的XML格式</returns>
public override string ToString()
{
StringBuilder sb = new StringBuilder();
Type t = this.GetType();
string result = null;
string fields;
//取得主键
PropertyInfo mainKey = t.GetProperty(MAINKEY);
if(mainKey == null)
return null;
//取得字段值
fields = getFields();
switch(outputType){
case ChangeType.INSERT:
result = string.Format("<request>\r\n\t<insert>{0}\r\n\t</insert>\r\n</request>", fields);
break;
case ChangeType.UPDATE:
result = string.Format("<request>\r\n\t<update>\r\n\t\t<tables>\r\n\t\t\t<table tablename='{0}'/>" +
"\r\n\t\t</tables>\r\n\t\t<condition>\r\n\t\t\t<cond tablename='{0}' " +
"fieldname='{1}' op='=' value='{2}'/>\r\n\t\t</condition>{3}\r\n\t</update>\r\n</request>",
t.Name,MAINKEY,mainKey.GetValue(this),fields);
break;
default:
break;
}
finishChange();
return result;
}
private string getFields()
{
Type t = this.GetType();
StringBuilder sb = new StringBuilder(string.Format("\r\n\t\t<fields>\r\n\t\t\t<{0}>", t.Name));
switch(outputType)
{
case ChangeType.INSERT:
PropertyInfo [] propertyInfos = t.GetProperties();
foreach(PropertyInfo propertyInfo in propertyInfos)
{
if(propertyInfo == null)continue;
if(propertyInfo.CanRead)
{//是公共属性
//特例:-不合法,字段使用_代替,这里需要恢复
string propertyInfoString = propertyInfo.Name.Replace('_', '-') ;
//特例:insert时候Upd不需要置值
if ("Upd".Equals(propertyInfoString))
{
sb.Append(string.Format("\r\n\t\t\t\t<{0}></{0}>", propertyInfoString));
}
else
{
sb.Append(string.Format("\r\n\t\t\t\t<{0}>{1}</{0}>", propertyInfoString, propertyInfo.GetValue(this)));
}
}
}
break;
case ChangeType.UPDATE:
foreach(string propertyName in _propertiesChangeMap.Keys)
{
if(propertyName == null)continue;
PropertyInfo propertyInfo = t.GetProperty(propertyName);
if(propertyInfo == null)continue;
if(propertyInfo.CanRead)
{//是公共属性
//特例:-不合法,字段使用_代替,这里需要恢复
string propertyInfoString = propertyInfo.Name.Replace('_', '-');
sb.Append(string.Format("\r\n\t\t\t\t<{0}>{1}</{0}>", propertyInfoString, propertyInfo.GetValue(this)));
}
}
break;
default:
break;
}
sb.Append(string.Format("\r\n\t\t\t</{0}>\r\n\t\t\t\t\r\n\t\t\t</fields>", t.Name));
return sb.ToString();
}
}
public enum ChangeType
{
INSERT,
UPDATE,
DELETE
}
}
int repId = 1;
int OrgGrpID = 11;
string stringResult = @"<request> <insert> <fields> <RepUser> <RepID>" + repId.ToString() + "</RepID><!--主键--> <OrgGrpID>" + OrgGrpID.ToString() + "</OrgGrpID><!--所属部门-->";
部分代码。