none
Deserialize XElement to A List<T> RRS feed

  • Question

  • Hi

    I have an XElement object which contains an xml document. I am trying to deserialize this back into a list that I can bind to a grid. I've put the xml document below. And put my classes that I'm trying to deserialize  to below. my properties are not being populated though. Any idea why not.

                using (MedistatDiscoveryDataContext dataContext = new MedistatDiscoveryDataContext(connectionString))
                {
                    var response = dataContext.MSVResponses.Where(m => m.Id == 1).FirstOrDefault();

                    MedistatDiscoveryDataContext.HPQMemberValidationResponse result = new MedistatDiscoveryDataContext.HPQMemberValidationResponse();

                    var serializer = new XmlSerializer(typeof(MedistatDiscoveryDataContext.HPQMemberValidationResponse));
                    result = (MedistatDiscoveryDataContext.HPQMemberValidationResponse)serializer.Deserialize(response.ResponseXML.CreateReader());

                }

          [XmlRoot("HPQMemberValidationResponse")]
            public class HPQMemberValidationResponse
            {
                [XmlElement(ElementName = "EnquiryNumber")]
                public string EnquiryNumber { get; set; }
                
                [XmlElement(ElementName = "ResponseDateTime")]
                public DateTime ResponseDateTime { get; set; }

                [XmlElement(ElementName = "ProviderOnKeyCareNetwork")]
                public string ProviderOnKeyCareNetwork { get; set; }

                [XmlElement("PatientDetails")]
                public List<PatientResponse> PatientResponses { get; set; }
            }

            public class PatientResponse
            {
                public string Title { get; set; }
               
            }

    <HPQMemberValidationResponse xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" schemaVersion="1">
      <ResponseDateTime xmlns="http://schemas.phisc.org.za/draft/policy">2016-10-18T10:47:27.956+02:00</ResponseDateTime>
      <EnquiryNumber xmlns="http://schemas.phisc.org.za/draft/policy">12285045</EnquiryNumber>
      <ProviderOnKeyCareNetwork xmlns="http://schemas.phisc.org.za/draft/policy">true</ProviderOnKeyCareNetwork>
      <PatientResponse xmlns="http://schemas.phisc.org.za/draft/policy">
        <PatientDetails>
          <Title>PROF</Title>
          <Initials>N</Initials>
          <FirstName>NHBMKFKJKFMC</FirstName>
          <Surname>MABMKFKJKFMC</Surname>
          <DateOfBirth>1956-05-07</DateOfBirth>
          <Sex>Male</Sex>
          <Scheme>UKZN Medical Scheme</Scheme>
          <Plan>STANDARD PLAN</Plan>
          <Status>The membership is valid and active</Status>
          <IDNumber>5605075744087</IDNumber>
          <MembershipNumber>247094791</MembershipNumber>
          <DependantNumber>0</DependantNumber>
          <DependantType>Principal party</DependantType>
          <EffectiveFrom>2007-08-01</EffectiveFrom>
          <EffectiveTo>9999-12-31</EffectiveTo>
        </PatientDetails>
      </PatientResponse>
      <PatientResponse xmlns="http://schemas.phisc.org.za/draft/policy">
        <PatientDetails>
          <Title>MR</Title>
          <Initials>S</Initials>
          <FirstName>SIBBMGMBKCLD</FirstName>
          <Surname>MABBMGMBKCLD</Surname>
          <DateOfBirth>2008-09-17</DateOfBirth>
          <Sex>Male</Sex>
          <Scheme>UKZN Medical Scheme</Scheme>
          <Plan>STANDARD PLAN</Plan>
          <Status>The membership is valid and active</Status>
          <IDNumber>0809175323082</IDNumber>
          <MembershipNumber>247094791</MembershipNumber>
          <DependantNumber>4</DependantNumber>
          <DependantType>Child</DependantType>
          <EffectiveFrom>2011-09-01</EffectiveFrom>
          <EffectiveTo>9999-12-31</EffectiveTo>
        </PatientDetails>
      </PatientResponse>
      <PatientResponse xmlns="http://schemas.phisc.org.za/draft/policy">
        <PatientDetails>
          <Title>MISS</Title>
          <Initials>R</Initials>
          <FirstName>REBBMJHKHHCH</FirstName>
          <Surname>CHBBMJHKHHCH</Surname>
          <DateOfBirth>2000-04-24</DateOfBirth>
          <Sex>Female</Sex>
          <Scheme>UKZN Medical Scheme</Scheme>
          <Plan>STANDARD PLAN</Plan>
          <Status>The membership is valid and active</Status>
          <IDNumber>0004240903080</IDNumber>
          <MembershipNumber>247094791</MembershipNumber>
          <DependantNumber>6</DependantNumber>
          <DependantType>Child</DependantType>
          <EffectiveFrom>2012-03-01</EffectiveFrom>
          <EffectiveTo>9999-12-31</EffectiveTo>
        </PatientDetails>
      </PatientResponse>
      <PatientResponse xmlns="http://schemas.phisc.org.za/draft/policy">
        <PatientDetails>
          <Title>DR</Title>
          <Initials>E</Initials>
          <FirstName>ERBBMJJKHDLC</FirstName>
          <Surname>RABBMJJKHDLC</Surname>
          <DateOfBirth>1969-02-08</DateOfBirth>
          <Sex>Female</Sex>
          <Scheme>UKZN Medical Scheme</Scheme>
          <Plan>STANDARD PLAN</Plan>
          <Status>The membership is valid and active</Status>
          <IDNumber>6902080000000</IDNumber>
          <MembershipNumber>247094791</MembershipNumber>
          <DependantNumber>5</DependantNumber>
          <DependantType>Spouse</DependantType>
          <EffectiveFrom>2012-03-01</EffectiveFrom>
          <EffectiveTo>9999-12-31</EffectiveTo>
        </PatientDetails>
      </PatientResponse>
    </HPQMemberValidationResponse>

    Tuesday, October 18, 2016 10:49 AM

Answers

  • Hi Kirk,

    For deserialize Xml document to list, I think there is something wrong in your class. I suggest you try below code:

        class ConvertXMLToList
        {
            public static void ConvertXMLToListT()
            {
               HPQMemberValidationResponse hpq;
               using (var reader = new StreamReader(@"D:\xx\App_Data\XMLDocument.xml")) //this is the parth of xml docuemnt
               {
                   XmlSerializer deserializer = new XmlSerializer(typeof(HPQMemberValidationResponse), new XmlRootAttribute("HPQMemberValidationResponse"));
                   hpq = (HPQMemberValidationResponse)deserializer.Deserialize(reader);
               }
    
            }
        }
        [XmlRoot("HPQMemberValidationResponse",Namespace="http://www.w3.org/2001/XMLSchema-instance")]
        public class HPQMemberValidationResponse
        {
            [XmlElement(ElementName = "EnquiryNumber", Namespace = "http://schemas.phisc.org.za/draft/policy")]
            public string EnquiryNumber { get; set; }
    
            [XmlElement(ElementName = "ResponseDateTime", Namespace = "http://schemas.phisc.org.za/draft/policy")]
            public DateTime ResponseDateTime { get; set; }
    
            [XmlElement(ElementName = "ProviderOnKeyCareNetwork", Namespace = "http://schemas.phisc.org.za/draft/policy")]
            public string ProviderOnKeyCareNetwork { get; set; }
    
            [XmlElement("PatientResponse", Namespace = "http://schemas.phisc.org.za/draft/policy")]
            public List<PatientResponse> PatientResponses { get; set; }
        }
    
        public class PatientResponse
        {
            [XmlElement("PatientDetails")]
            public List<PatientDetails> PatientDetails { get; set; }
        }
        public class PatientDetails
        {
            [XmlElement("Title")]
            public string Title { get; set; }
        }
    

    Best Regards,

    Edward


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Wednesday, October 19, 2016 5:43 AM

All replies

  • What is wrong with Linq-To-XML  where you would use Linq to query the xml and project out concrete objects?

     
    Tuesday, October 18, 2016 7:27 PM
  • Hi Kirk,

    For deserialize Xml document to list, I think there is something wrong in your class. I suggest you try below code:

        class ConvertXMLToList
        {
            public static void ConvertXMLToListT()
            {
               HPQMemberValidationResponse hpq;
               using (var reader = new StreamReader(@"D:\xx\App_Data\XMLDocument.xml")) //this is the parth of xml docuemnt
               {
                   XmlSerializer deserializer = new XmlSerializer(typeof(HPQMemberValidationResponse), new XmlRootAttribute("HPQMemberValidationResponse"));
                   hpq = (HPQMemberValidationResponse)deserializer.Deserialize(reader);
               }
    
            }
        }
        [XmlRoot("HPQMemberValidationResponse",Namespace="http://www.w3.org/2001/XMLSchema-instance")]
        public class HPQMemberValidationResponse
        {
            [XmlElement(ElementName = "EnquiryNumber", Namespace = "http://schemas.phisc.org.za/draft/policy")]
            public string EnquiryNumber { get; set; }
    
            [XmlElement(ElementName = "ResponseDateTime", Namespace = "http://schemas.phisc.org.za/draft/policy")]
            public DateTime ResponseDateTime { get; set; }
    
            [XmlElement(ElementName = "ProviderOnKeyCareNetwork", Namespace = "http://schemas.phisc.org.za/draft/policy")]
            public string ProviderOnKeyCareNetwork { get; set; }
    
            [XmlElement("PatientResponse", Namespace = "http://schemas.phisc.org.za/draft/policy")]
            public List<PatientResponse> PatientResponses { get; set; }
        }
    
        public class PatientResponse
        {
            [XmlElement("PatientDetails")]
            public List<PatientDetails> PatientDetails { get; set; }
        }
        public class PatientDetails
        {
            [XmlElement("Title")]
            public string Title { get; set; }
        }
    

    Best Regards,

    Edward


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Wednesday, October 19, 2016 5:43 AM
  • Thanks its working now. I was missing the namespaces.
    Wednesday, October 19, 2016 11:38 AM