歡迎跟我連絡

本頁最下方有Web MSN可以直接跟我交談喔!
免安裝程式...哈哈 歡迎聊天

2008年7月14日 星期一

物件的XML序列化(Serialize) 與 反序列化(Deserialize)

由於工作關係,Server端與Client端之間的交易(Transcation:TX)是XML的格式.
Server端的程式都是C++寫的,在Compiled過程中有隻Utility會將TX的Header File(*.H),轉譯成TAG File,再透過API將TX Struct轉成XML,送給Client.
Server端,因為有API所以還OK,但是自行開發的一些Client沒有API,就得做苦工進行組XML或解XML的工作,一直覺的很麻煩...-_-"
日前看到物件序列化(Serialize)與反序列化(Deserialize)的技術,開始作手研究,哈哈,終於解決的我的困擾,能夠開始高速開發程式.
先寫一隻Parser將Header File(*.H)轉成C#的Class File,再利用XmlSerializer進行,序列化及反序列化的動作,很快的解決我的問題了.

以我的TX為例:
先將TX物件化...
using System;
using System.Collections;
using System.Text;
using System.Xml;
using System.Xml.Serialization;

namespace 物件序列化與TX
{
public class APIQCRRD
{
public class APIQCRRD_I
{
[XmlRoot("transaction")]
public class transaction
{
public string trx_id;
public string type_id;
public string crr_id;
public string lot_id;
public string splt_id;
public string inpr_sht_flg;
public string logon_eqpt_id;
public string logon_date;
public string logon_time;
public string inq_type;
public string all_shop;
public string offline_report_type;
public transaction()
{
trx_id = "APIQCRRD";
type_id = "I";
crr_id=string.Empty;
lot_id=string.Empty;
splt_id=string.Empty;
inpr_sht_flg=string.Empty;
logon_eqpt_id=string.Empty;
logon_date=string.Empty;
logon_time=string.Empty;
inq_type=string.Empty;
all_shop=string.Empty;
offline_report_type = string.Empty;
}
}
}

public class APIQCRRD_O
{
[XmlRoot("transaction")]
public class transaction
{
public string trx_id;
public string type_id;
public string rtn_code;
public string crr_id;
public string crr_stat;
public string valid_flg;
public string sht_cnt;
public string pnl_cnt;
public string pnl_sht_cnt;
public string clup_flg;
public string clup_date;
public string clup_time;
public string real_emp;
public string product_id;
public string ec_code;
public string route_id;
public string ope_no;
public string ope_id;
public string proc_id;
public string eqpt_id;
public string pv_eqpt_id;
public string pep_lvl;
public string recipe_id;
public string pv_ope_no;
public string cr_ope_no;
public string wip_bank_flg;
public string shp_bank_flg;
public string crrg_id;
public string crr_use_cnt;
public string max_crr_use_cnt;
public string max_use_over_flg;
public string crr_use_time;
public string max_crr_use_time;
public string max_time_over_flg;
public string ptt_grade;
public string crr_grade;
public string mes_id;
public string crr_cate;
public string max_sht_cnt;
public string position;
public string zone_id;
public string fr_eqpt_id;
public string fr_port_id;
public string to_eqpt_id;
public string to_zone_id;
public string xf_cmd_stat;
public string xfer_stat;
public string prty;
public string crr_cate_ext;
public string crr_purpose;
public string comment;
public string qrs_ary_cnt;
public string sht_ary_cnt;
private ArrayList Oary1;
[XmlElement("oary1")]
public OARY[] oary1
{
get
{
OARY[] oary_1 = new OARY[Oary1.Count];
Oary1.CopyTo(oary_1);
return oary_1;
}
set
{
if (value == null) return;
OARY[] oary_1 = (OARY[])value;
Oary1.Clear();
foreach (OARY o in oary_1)
Oary1.Add(o);
}

}
public int AddItem(OARY oary_1)
{
return Oary1.Add(oary_1);
}
public string vryope_flg;
public string crr_own;
public string rgst_own;
public string mfg_plan_comp_date;
public string pln_cmp_date;
public string pln_cmp_time;
public string no_xfer_flg;
public string ppbox_id;
public string crr_set_code;
public string ftflag;
public string no_xfer_comment;

public transaction()
{
Oary1 = new ArrayList();
}

}
}

public class OARY
{
public string sht_id;
public string sht_stat;
public string lot_id;
public string splt_id;
public string slot_no;
public string reproc_flg;
public string sht_note_flg;
public string sheetjdg;
public string pnl_cnt;
public string pnl_grade;
public string ptt_group;
public string crr_grade;
public string ito_flg;
public string mtrl_product_id;
public string curr_eqp_attr;
public OARY()
{
}
public OARY(string Sht_id,
string Sht_stat,
string Lot_id,
string Splt_id,
string Slot_no,
string Reproc_flg,
string Sht_note_flg,
string Sheetjdg,
string Pnl_cnt,
string Pnl_grade,
string Ptt_group,
string Crr_grade,
string Ito_flg,
string Mtrl_product_id,
string Curr_eqp_attr)
{
sht_id = Sht_id;
sht_stat = Sht_stat;
lot_id = Lot_id;
splt_id = Splt_id;
slot_no = Slot_no;
reproc_flg = Reproc_flg;
sht_note_flg = Sht_note_flg;
sheetjdg = Sheetjdg;
pnl_cnt = Pnl_cnt;
pnl_grade = Pnl_grade;
ptt_group = Ptt_group;
crr_grade = Crr_grade;
ito_flg = Ito_flg;
mtrl_product_id = Mtrl_product_id;
curr_eqp_attr = Curr_eqp_attr;
}
}
}
}

然後MakeTX(序列化)
private void button_Serialize_Click(object sender, EventArgs e)
{
//Make TX
APIQCRRD.APIQCRRD_I.transaction input = new APIQCRRD.APIQCRRD_I.transaction();
input.crr_id = "AA1234";
input.all_shop = "Y";

// Serialization
XmlSerializer s = new XmlSerializer(typeof(APIQCRRD.APIQCRRD_I.transaction));
TextWriter w = new StreamWriter(物件序列化與TX.Properties.Settings.Default.INPUT_LOG);
s.Serialize(w, input);
w.Close();

//Dispaly in TextBox
TextReader r = new StreamReader(物件序列化與TX.Properties.Settings.Default.INPUT_LOG);
textBox_Serialize.Text = r.ReadToEnd();//不去除空Element
r.Close();
}

然後GetTX(反序列化)
private void button_Deserialize_Click(object sender, EventArgs e)
{
// Deserialization
XmlSerializer s = new XmlSerializer(typeof(APIQCRRD.APIQCRRD_O.transaction));
APIQCRRD.APIQCRRD_O.transaction txResult = new APIQCRRD.APIQCRRD_O.transaction();
TextReader r = new StreamReader(物件序列化與TX.Properties.Settings.Default.OUTPUT_LOG);
txResult = (APIQCRRD.APIQCRRD_O.transaction)s.Deserialize(r);
r.Close();
}

短短的幾行就完成的....



雨季說:
以上程式的程式碼上有很多Project的設定,並無法直接執行..
除此之外,程式碼是正確無誤的...
有興趣的話,可以留言跟我要原始碼...

0 個回應:

MSN狀態(我在線上時,可以跟我交談喔)