0

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></CrefId>
                                                <EntityKey></EntityKey>
                                                <EntityId></EntityId>
                                                <EntityId2/>
                                                <EntityName></EntityName>
                                                <EntityDOBDOC></EntityDOBDOC>
                                                <EntityGroupCode></EntityGroupCode>
                                                <EntityState/>
                                                <EntityNationality></EntityNationality>
                                                <CcrisNote/>
                                            </item>
                                        </ccris_identity>
                                        <spga_identity>
                                            <SRefId></SRefId>
                                            <Name></Name>
                                            <IdCode></IdCode>
                                            <SpgaId></SpgaId>
                                            <Hit></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."

1
  • 4
    " Just to confirm, Is it correct?" - well, does it work? Commented Nov 7, 2022 at 10:31

1 Answer 1

1

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

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.