Home > Software engineering >  Deserializing XML from String C#
Deserializing XML from String C#

Time:11-08

I am receiving XML strings,and would like to convert these to C# objects. Just to confirm, Is it correct?

This is my class :

[XmlRoot("xml")]
    public class GenCrfeidData
    {
        [XmlAttribute("ccris_identity")]
        public GenCrfeidCCRIS_Identity ccris_identity { get; set; }
        [XmlAttribute("spga_identity")]
        public GenCrfeIDSPGA_Identity spga_identity { get; set; }
    }
    public class GenCrfeidCCRIS_Identity
    {
        [XmlAttribute("item")]
        public  List<GenCrfeIDItem> item {get; set;}
    }
    public class GenCrfeIDItem
    {
        [XmlAttribute("CRefId")]
        public string CRefId { get; set; }

        [XmlAttribute("EntityKey")]
        public string EntityKey { get; set; }

        [XmlAttribute("EntityId")]
        public string EntityId { get; set; }

        [XmlAttribute("EntityId2")]
        public string EntityId2 { get; set; }

        [XmlAttribute("EntityName")]
        public string EntityName { get; set; }

        [XmlAttribute("EntityDOBDOC")]
        public string EntityDOBDOC { get; set; }

        [XmlAttribute("EntityGroupCode")]
        public string EntityGroupCode { get; set; }

        [XmlAttribute("EntityState")]
        public string EntityState { get; set; }

        [XmlAttribute("EntityNationality")]
        public string EntityNationality { get; set; }

        [XmlAttribute("CcrisNote")]
        public string CcrisNote { get; set; }
    }

    public class GenCrfeIDSPGA_Identity
    {
        [XmlAttribute("SRefId")]
        public string SRefId { get; set; }

        [XmlAttribute("Name")]
        public string Name { get; set; }

        [XmlAttribute("IDCode")]
        public string IDCode { get; set; }

        [XmlAttribute("SpgaId")]
        public string SpgaId { get; set; }

        [XmlAttribute("Hit")]
        public string Hit { get; set; }
    }

This is my code :

string testData = @"<xml>
                                        <ccris_identity>
                                            <item>
                                                <CrefId>1123603</CrefId>
                                                <EntityKey>8869901</EntityKey>
                                                <EntityId>670828-02-6001</EntityId>
                                                <EntityId2/>
                                                <EntityName>MOH AMAD ASRI BIN HAJI ABUBAKAR</EntityName>
                                                <EntityDOBDOC>1976-09-01</EntityDOBDOC>
                                                <EntityGroupCode>11</EntityGroupCode>
                                                <EntityState/>
                                                <EntityNationality>MY</EntityNationality>
                                                <CcrisNote/>
                                            </item>
                                        </ccris_identity>
                                        <spga_identity>
                                            <SRefId>42978</SRefId>
                                            <Name>MOHAMAD ASRI BIN HAJI ABU BAKAR</Name>
                                            <IdCode>C</IdCode>
                                            <SpgaId>670828026001</SpgaId>
                                            <Hit>1</Hit>
                                        </spga_identity>
                                    </xml>";

                XmlSerializer serializer = new XmlSerializer(typeof(GenCrfeidData));
                using (StringReader reader = new StringReader(testData))
                {
                    result = (GenCrfeidData)serializer.Deserialize(reader);
                }

And i got an error "Cannot serialize member 'ccris_identity' of type Test.Domain.Business.Entities.GenCrfeidCCRIS_Identity. XmlAttribute/XmlText cannot be used to encode complex types."

CodePudding user response:

<ccris_identity> is an element, not an attribute; you need [XmlElement("ccris_identity")]; ditto basically everything.

(an attribute is bar in <example bar="123"/>)

  • Related