DataContract Serializer不反序列化List

时间:2013-10-28 17:41:28

标签: c# wcf datacontractserializer

我尝试使用datacontract序列化程序将以下XML反序列化为类。

"<ns2:MemberCarePlanModel xmlns:ns2=\"http://v3.ua.test.com\">
 <ns2:memberGoal><ns2:goalDescription>Has received diabetes self-management overview</ns2:goalDescription>
 <ns2:memberId>111111111</ns2:memberId>
 <ns2:goalId>be406994-47a6-4802-8dd6-9616bfbaaead</ns2:goalId>
 </ns2:memberGoal>
 </ns2:MemberCarePlanModel>"

我的DataContract类代码是

  namespace Medventive.Registry.Service.ServiceImplementation.VITALServices
  {
[DataContract(Namespace = @"http://v3.ua.test.com")]
[KnownType(typeof(MemberCareProblemModel))]
[KnownType(typeof(MemberGoalActivityModel))]
public class MemberGoalModel 
{
    /*
     * <xs:sequence>
    <xs:element maxOccurs="unbounded" minOccurs="0" name="careProblem" nillable="true" type="tns:MemberCareProblemModel"/>
    <xs:element maxOccurs="unbounded" minOccurs="0" name="goalActivity" nillable="true" type="tns:MemberGoalActivityModel"/>
    <xs:element minOccurs="0" name="goalDescription" type="xs:string"/>
    <xs:element minOccurs="0" name="memberId" type="xs:string"/>
    <xs:element minOccurs="0" name="goalId" type="xs:string"/>
    </xs:sequence>
     */

    //[DataMember(EmitDefaultValue = false, Name = "careProblem", Order = 1)]
    //public List<MemberCareProblemModel> careProblem { get; set; }

    //[DataMember(EmitDefaultValue = false, Name = "goalActivity", Order = 2)]
    //public List<MemberGoalActivityModel> goalActivity { get; set; }

     [DataMember(EmitDefaultValue = false, Name = "goalDescription", Order = 1)]
     public string goalDescription { get; set; }

     [DataMember(EmitDefaultValue = false, Name = "memberId", Order = 2)]
     public string memberId { get; set; }

     [DataMember(EmitDefaultValue = false, Name = "goalId", Order = 3)]
     public string goalId { get; set; }
    }
 }


namespace Medventive.Registry.Service.ServiceImplementation.VITALServices
{
[DataContract(Namespace = @"http://v3.ua.test.com", Name="MemberCarePlanModel")]
[KnownType(typeof(MemberGoalModel))]
public class MemberCarePlanModel
{

    [DataMember(EmitDefaultValue = false, Name = "memberGoal", Order = 1)]
    public List<MemberGoalModel> memberGoal { get; set; }
}
}

我无法弄清楚为什么这段代码不会反序列化列表或成员目标的问题。如果在carePlan类中有一个memberGoal类,但是列表失败,则工作正常。

下面是我的反序列化代码。我将xml转换为内存流并将其传递给我的反序列化器。

     protected static T Deserialize<T>(Stream stream)
    {
        DataContractSerializer ser = new DataContractSerializer(typeof(T));
        XmlReaderSettings rs = new XmlReaderSettings
        {
            ConformanceLevel = ConformanceLevel.Fragment,
        };
        XmlReader r = XmlReader.Create(stream, rs);
        while (!r.EOF)
        {
            T obj = (T)ser.ReadObject(r);
            return obj;
        }

        return default(T);

    }

1 个答案:

答案 0 :(得分:0)

显然,DataContract Serializer不会序列化列表对象。

我用以下代码修复了上述问题

var memberGoals = from v in doc.Root.Descendants(Common.Constants.NameSpace + "memberGoal")
                              select new MemberGoalModel
                              {
                                  goalDescription = v.Element(Common.Constants.NameSpace + "goalDescription").Value,
//Assign Other Properties here. 

                              };
            //return Deserialize<MemberCarePlanModel>(ms);
            return memberGoals.ToList()