2012/05/14

20120514-程式-Class集合序列化XML的方法


參考文:http://goo.gl/Qm5zX
=======================================================
這一樣的層級概念,因觀念錯誤,我試了很久才試出來,
要呈現此架構,在於它ebAcctListInqRs Class 集合的宣告方式
而不是額外再宣告一個 REQBDY Class 後,再宣告一個List<BDYREC > BDYRECs

================================
正確版
================================
宣告法: [XmlArray("REQBDY"), XmlArrayItem(typeof(BDYREC), ElementName = "BDYREC")]

 [Serializable, XmlRoot("ebAcctListInqRs", Namespace = "http://ns.chinatrust.com.tw/XSD/CTCB/ESB/Message/BSMF/ebAcctListInqRs/01")]
    public class ebAcctListInqRs
    {
        private REQHDR _REQHDR = new REQHDR();
        //private REQBDY _REQBDY = new REQBDY();
        private RESTLR _RESTLR = new RESTLR();
        [XmlElement("REQHDR",Namespace = "http://ns.chinatrust.com.tw/XSD/CTCB/ESB/Message/BSMF/ebAcctListInqRs/01")]
        public REQHDR REQHDR
        {
            get { return _REQHDR; }
            set { _REQHDR = value; }
        }
        [XmlArray("REQBDY"), XmlArrayItem(typeof(BDYREC), ElementName = "BDYREC")]
        public List BDYRECs { get; set; }
        [XmlElement("RESTLR", Namespace = "http://ns.chinatrust.com.tw/XSD/CTCB/ESB/Message/BSMF/ebAcctListInqRs/01")]
        public RESTLR RESTLR
        {
            get { return _RESTLR; }
            set { _RESTLR = value; }
        }
    }

============================================
錯誤寫法
============================================
 [Serializable, XmlRoot("ebAcctListInqRs", Namespace = "http://ns.chinatrust.com.tw/XSD/CTCB/ESB/Message/BSMF/ebAcctListInqRs/01")]
    public class ebAcctListInqRs
    {
        private REQHDR _REQHDR = new REQHDR();
        private REQBDY _REQBDY = new REQBDY();
        private RESTLR _RESTLR = new RESTLR();
        [XmlElement("REQHDR",Namespace = "http://ns.chinatrust.com.tw/XSD/CTCB/ESB/Message/BSMF/ebAcctListInqRs/01")]
        public REQHDR REQHDR
        {
            get { return _REQHDR; }
            set { _REQHDR = value; }
        }
        [XmlElement("REQBDY", Namespace = "http://ns.chinatrust.com.tw/XSD/CTCB/ESB/Message/BSMF/ebAcctListInqRs/01")]
        public REQBDY REQBDY
        {
            get { return _REQBDY; }
            set { _REQBDY = value; }
        }
        [XmlElement("RESTLR", Namespace = "http://ns.chinatrust.com.tw/XSD/CTCB/ESB/Message/BSMF/ebAcctListInqRs/01")]
        public RESTLR RESTLR
        {
            get { return _RESTLR; }
            set { _RESTLR = value; }
        }
    }

再宣告出 REQBDY (以下寫法是無法gen出它下面的item的)
  [Serializable, XmlRoot("REQBDY", Namespace = "http://ns.chinatrust.com.tw/XSD/CTCB/ESB/Message/BSMF/ebAcctListInqRs/01")]
    public class REQBDY
    {
        private BDYREC _BDYREC;
        /// 
        /// BDYREC資訊列表
        /// 
        [XmlElement("BDYREC", Namespace = "http://ns.chinatrust.com.tw/XSD/CTCB/ESB/Message/BSMF/ebAcctListInqRs/01")]
        public BDYREC BDYRECs
        {
            get { return _BDYREC; }
            set { _BDYREC = value; }
        }
        //[XmlArray("BDYRECList"), XmlArrayItem(typeof(BDYREC), ElementName = "BDYREC")]
        //public List BDYRECs { get; set; }
    }

==========================================
Class 與 XML 間的互轉程式碼
==========================================
        public static string ToXml(object Object, XmlSerializerNamespaces Namespace)
        {
            string result = string.Empty;
            XmlSerializer xml = null;
            StreamWriter writer = null;

            using (MemoryStream mstream = new MemoryStream())
            {
                //寫入
                xml = new XmlSerializer(Object.GetType());
                writer = new StreamWriter(mstream, Encoding.UTF8);
                xml.Serialize(writer, Object, Namespace);
                writer.Flush();

                //讀
                mstream.Position = 0;
                StreamReader reader = new StreamReader(mstream);
                result = reader.ReadToEnd();

                writer.Close();
                reader.Close();
            }

            return result;
        }
        public static object ToObject(XmlReader xmlReader,Type type)
        {
            object result = null;
            XmlSerializer xml = new XmlSerializer(type);
            result = xml.Deserialize(xmlReader);
            return result;
        }





0 意見 :

張貼留言