Convert XML file to C# object with nested Lists

I have this XML file that I am trying to deserialize into a C# object, Closing Tabke tag wont show up but thats besides the point:

<Table>
<ConditionSets>
    <ConditionSet Name="All Coastal Exposures">
        <Condition Field="State" Values="GA,FL,NC,AL"/>
        <Condition Field="Client" Values="Default,ICAT,Olympus,SJIC"/>
        <Condition Field="Company" Values="GM,ACME,NWGF,DSFN,DSIC"/>
        <Condition Field="Product" Values="HO,FO"/>
        <Condition Field="Program" Values="*"/>
        <Condition Field="LineOfBusiness" Values="HO,FO,DF,IM"/>
        <Condition Field="TransactionType" Values="NB,RN,CH,RR,RW,RE"/>
        <Condition Field="TransactionMode" Values="Q,A"/>
        <Condition Field="UserType" Values="A,E"/>
        <Condition Field="Process" Values="Q,R,I"/>
        <Condition Field="Environment" Values="DEV,QA,UAT,PROD"/>
        <Condition Field="System" Values="BASE,SBIC,DSFM,NCIC"/>
        <Condition Field="Page" Values="Applicant,Completion"/>
        <Condition Field="Expression" Values="PolTransPolicy.BillingMethodCd='D'"/>
    </ConditionSet>
    <ConditionSet Name="Midwest Region">
        <Condition Field="State" Values="IL,IO,WI,MI,MN"/>
        <Condition Field="Client" Values="*"/>
        <Condition Field="Company" Values="GMIC,BMIC"/>
        <Condition Field="Product" Values="HO,FO,DF"/>
        <Condition Field="Program" Values="*"/>
        <Condition Field="LineOfBusiness" Values="HO,FO,DF,IM,PU"/>
        <Condition Field="TransactionType" Values="NB,RN,CH,RR,RW,RE"/>
        <Condition Field="TransactionMode" Values="Q,A"/>
        <Condition Field="UserType" Values="A,E"/>
        <Condition Field="Process" Values="Q,R,I"/>
        <Condition Field="Environment" Values="DEV,QA"/>
        <Condition Field="System" Values="BASE,GMIC,BMIC"/>
        <Condition Field="Page" Values="*"/>
        <Condition Field="Expression" Values="PolTransPolicy.BillingMethodCd='D'"/>
    </ConditionSet>
</ConditionSets>

I can't seem to get it to deserialize correctly because it is a list of lists. How would I set this up correctly in a C# object to deserialize?

This is the class im using to deserialize

[XmlRoot("Table")]
    public class Table
    {
        [XmlArray("ConditionSets")]
        [XmlArrayItem("ConditionSet")]
        public List<ConditionSet> ConditionSets { get; set; }
    }

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

        [XmlArray("Conditions")]
        [XmlArrayItem("Condition")]
        public List<Condition> Conditions { get; set; }
    }

    public class Condition
    {
        [XmlAttribute("Field")]
        public string Field { get; set; }

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


Read more here: https://stackoverflow.com/questions/66323722/convert-xml-file-to-c-sharp-object-with-nested-lists

Content Attribution

This content was originally published by Austin Moen at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: