2020年2月19日 星期三

XML反序列化方法 根節點

因為一個小失誤又把自己卡很久
紀錄一下避免又發生 反序列化節點

先做 反序列化方法
/// <summary>
        /// 將XML格式字串反序列化成物件
        /// </summary>
        /// <typeparam name="T">物件型別</typeparam>
        /// <param name="xmlString">XML格式字串</param>
        /// <returns>反序列化後的物件</returns>
        public T Deserialize<T>(string xmlString) where T : class
        {
            XmlSerializer deserializer = new XmlSerializer(typeof(T));
            using (TextReader reader = new StringReader(xmlString))
            {
                object deserializationObj = deserializer.Deserialize(reader);
                return deserializationObj as T;
            };
        }


建立相對應的 XML

 [XmlRoot("transaction")]//這個超級重要 節點一定要設
    public class csCSTData
    {
        public string trx_id { get; set; }
        public string type_id { get; set; }
        public string rtn_code { get; set; }
        public string crr_id { get; set; }
        public string pnl_type { get; set; }
        public string pfcd { get; set; }
        public string pid { get; set; }
        public string owner_id { get; set; }
        public string end_code { get; set; }
        public string proc_id { get; set; }
        public string dest_shop { get; set; }
        public string pnl_sht_cnt { get; set; }
        public string sht_cnt { get; set; }
        [XmlElement("oary1")]//這個超級重要 節點一定要設
        public List<csSlotData> oary1 = new List<csSlotData>();
      } 
 public class csSlotData
    {
        public string sht_no { get; set; }
        public string slot_no { get; set; }
        public string sht_id { get; set; }
        public string real_sht_id { get; set; }
        public string customer_type { get; set; }
        public string sht_grade { get; set; }
        public string cf_sht_id { get; set; }
        public string pnl_mode { get; set; }
        public string group_id { get; set; }
        public string pfcd { get; set; }
        public string worder_id { get; set; }
        public string owner_id { get; set; }
        public string q_time_flag { get; set; }
        public string panel_angle { get; set; }
        public string rwk_cnt { get; set; }
        public string mes_link_key { get; set; }
        public string qtap_flag { get; set; }
        public string pi_insp_flg { get; set; }
        public string over_bake_flg { get; set; }
        public string seal_review_flg { get; set; }
        public string mang_flg { get; set; }

        public string tft_repair_grade { get; set; }
        public string door_open_flg { get; set; }//>N</door_open_flg>
        public string force_stay_flg { get; set; }//>N</force_stay_flg>
        public string seal_repair_flg { get; set; }//>N</seal_repair_flg>
        public string mura_flg { get; set; }//>N</mura_flg>
        public string qrs_avl_flg { get; set; }//>Y</qrs_avl_flg>
        public string loi1_ope_mode { get; set; }//>10</loi1_ope_mode>
        public string loi2_ope_mode { get; set; }//>00</loi2_ope_mode>
        public string rsn_cnt { get; set; }//00</rsn_cnt>
    }
使用反序列化

string stFile = "D:\\Data";
                    if(File.Exists(stFile))
                    {
                        MyXml.stXMLPath = stFile;
                        XmlDocument xmlDoc = new XmlDocument();
                        xmlDoc.Load(stFile);
                        csGlobal.CSTData = MyXml.Deserialize<csCSTData>(xmlDoc.OuterXml);
                 
                     
                    }



沒有留言:

張貼留言