1
Vote

YAXLib can't deserialize Collection with no containing elements

description

I need deserialize a xml string, and i created following class
[YAXSerializeAs("message")]
    public class GetDepartmentResponse
    {
        [YAXSerializeAs("user")]
        public MessageUser User { get; set; }

        [YAXSerializeAs("body")]
        public MessageBody Body { get; set; }

        public class MessageUser
        {
            [YAXSerializeAs("time")]
            public string Time { get; set; }

            [YAXSerializeAs("command")]
            public string Command { get; set; }

            [YAXSerializeAs("isresult")]
            public byte Isresult { get; set; }

            [YAXSerializeAs("mobilecode")]
            public uint Mobilecode { get; set; }

            [YAXSerializeAs("type")]
            public string Type { get; set; }

            [YAXSerializeAs("business")]
            public string Business { get; set; }

            [YAXSerializeAs("serialid")]
            public int Serialid { get; set; }
        }

        public class MessageBody
        {
            [YAXSerializeAs("count")]
            public byte Count { get; set; }

            [YAXSerializeAs("current")]
            public byte Current { get; set; }

            [YAXSerializeAs("result")]
            public byte Result { get; set; }

            [YAXSerializeAs("cause")]
            public string Cause { get; set; }

            [YAXCollection(YAXCollectionSerializationTypes.RecursiveWithNoContainingElement)]
            [YAXSerializeAs("departs")]
            public MessageBodyDeparts[] Departs { get; set; }
        }

        [YAXSerializeAs("departs")]
        public class MessageBodyDeparts
        {
            [YAXSerializeAs("companyId")]
            public uint CompanyId { get; set; }

            [YAXSerializeAs("departId")]
            public uint DepartId { get; set; }

            [YAXSerializeAs("departVasId")]
            public uint DepartVasId { get; set; }

            [YAXSerializeAs("departLevel")]
            public byte DepartLevel { get; set; }

            [YAXSerializeAs("departName")]
            public string DepartName { get; set; }

            [YAXSerializeAs("parentDepart")]
            public string ParentDepart { get; set; }

            [YAXSerializeAs("orderNo")]
            public byte OrderNo { get; set; }
        }

        public static GetDepartmentResponse Parse(string xml)
        {
            var ser = new YAXSerializer(typeof(GetDepartmentResponse), YAXExceptionHandlingPolicies.DoNotThrow);
            return ser.Deserialize(xml) as GetDepartmentResponse;
        }
    }
when i deserialize into obj, the departs array have only 7(expected 8) elements and all elements contain null properties.
var xml =
                "<message><user><time>2015-02-27 15:35:09</time><command>OAQueryDepartmentResp</command><isresult>1</isresult><mobilecode>01214006034</mobilecode><type>pda</type><business>XN</business><serialid>1508608</serialid></user><body><count>8</count><current>8</current><result>1</result><cause>部门查询成功!</cause><departs><companyId>915722</companyId><departId>850005</departId><departVasId>103651</departVasId><departLevel>1</departLevel><departName>Ban giam doc</departName><parentDepart>850001</parentDepart><orderNo>0</orderNo></departs><departs><companyId>915722</companyId><departId>850008</departId><departVasId>103672</departVasId><departLevel>1</departLevel><departName>ChiNhanh</departName><parentDepart>850001</parentDepart><orderNo>0</orderNo></departs><departs><companyId>915722</companyId><departId>850001</departId><departVasId>103643</departVasId><departLevel>0</departLevel><departName>Cong Ty Co Phan Dau Tu Quoc Te VNPT</departName><parentDepart></parentDepart><orderNo>0</orderNo></departs><departs><companyId>915722</companyId><departId>850004</departId><departVasId>103650</departVasId><departLevel>1</departLevel><departName>Phong cham soc khach hang</departName><parentDepart>850001</parentDepart><orderNo>0</orderNo></departs><departs><companyId>915722</companyId><departId>850007</departId><departVasId>103653</departVasId><departLevel>1</departLevel><departName>Phong hanh chinh nhan su</departName><parentDepart>850001</parentDepart><orderNo>0</orderNo></departs><departs><companyId>915722</companyId><departId>850003</departId><departVasId>103649</departVasId><departLevel>1</departLevel><departName>Phong kinh doanh</departName><parentDepart>850001</parentDepart><orderNo>0</orderNo></departs><departs><companyId>915722</companyId><departId>850002</departId><departVasId>103648</departVasId><departLevel>1</departLevel><departName>Phong ky thuat</departName><parentDepart>850001</parentDepart><orderNo>0</orderNo></departs><departs><companyId>915722</companyId><departId>850006</departId><departVasId>103652</departVasId><departLevel>1</departLevel><departName>Test</departName><parentDepart>850001</parentDepart><orderNo>0</orderNo></departs></body></message>";
            var getDepartmentResponse = GetDepartmentResponse.Parse(xml);
            Console.WriteLine("Expected: '{0}', Actual: '{1}'", "Ban giam doc", getDepartmentResponse.Body.Departs[0].DepartName);
            Console.ReadLine();
I attach my test project Test.rar
Can you help me? Thanks!

file attachments

comments