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 のクラスが 1 つある場合は正常に動作しますが、リストでは失敗します。
以下は私の逆シリアル化コードです。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);
}