0

I met a very complicated XML structure, I think it's like nested 2D array. But my definition seems not working. I have an Xml structure like below

<Rate>
<GROUP>
    <GROUPNAME>Company</GROUPNAME>
    <COUNTY>Dawson</COUNTY>
</GROUP>
<EMPLOYEES>
    <MEMBERS>
        <MEMBER>
            <SEQUENCENUM>1</SEQUENCENUM>
            <GENDER>M</GENDER>
            <RELATIONSHIP>Father</RELATIONSHIP>
        </MEMBER>
        <MEMBER>
            <SEQUENCENUM>2</SEQUENCENUM>
            <GENDER>F</GENDER>
            <RELATIONSHIP>Mother</RELATIONSHIP>
        </MEMBER>
    </MEMBERS>
    <MEMBERS>
        <MEMBER>
            <SEQUENCENUM>1</SEQUENCENUM>
            <GENDER>M</GENDER>
            <RELATIONSHIP>Father</RELATIONSHIP>
        </MEMBER>
        <MEMBER>
            <SEQUENCENUM>2</SEQUENCENUM>
            <GENDER>Y</GENDER>
            <RELATIONSHIP>Mother</RELATIONSHIP>
        </MEMBER>
    </MEMBERS>
</EMPLOYEES>

And I defined three classes for this structure

[XmlRoot("Rate")]
public class Rate
{
  [XmlElement("GROUP")]
  public GroupInfo Group{get; set;}

  [XmlArray("EMPLOYEES")]
  [XmlArrayItem("MEMBERS", typeof(Members))]
  public List<Members> Employees{get; set;}
}

And this one

[XmlRoot("EMPLOYEES")]
public class Members
{
   [XmlArray("MEMBERS")]
   [XmlArrayItem("MEMBER", typeof(MemberInfo))]
   public List<MemberInfo> Members{get; set;}
}

And this one:

[XmlRoot("Member")]
public class MemberInfo
{
    public string SequenceNum{get; set;}
    
    [XmlElement("GENDER")]
    public string Gender{get; set;}
    
    [XmlElement("RELATIONSHIP")]
    public string Relationship{get; set}
}

Do you guys have any great idea for this? For this nested array?

1 Answer 1

1

Add NestingLevel = 1 to your XmlArrayItem attribute.

This class will deserialize your XML.

[Serializable]
[XmlType(AnonymousType = true)]
[XmlRoot(Namespace = "", IsNullable = false)]
public class Rate
{
    [XmlElement("GROUP")]
    public Group Group { get; set; }

    [XmlArrayItem("MEMBERS", IsNullable = false)]
    [XmlArrayItem("MEMBER", IsNullable = false, NestingLevel = 1)]
    public Member[][] EMPLOYEES { get; set; }
}

[XmlType(AnonymousType = true)]
public class Group
{
    [XmlElement("GROUPNAME")]
    public string Name { get; set; }

    [XmlElement("COUNTY")]
    public string Country { get; set; }
}

[XmlType(AnonymousType = true)]
public class Member
{
    [XmlElement("SEQUENCENUM")]
    public byte Sequencenum { get; set; }

    [XmlElement("GENDER")]
    public string Gender { get; set; }

    [XmlElement("RELATIONSHIP")]
    public string Relationship { get; set; }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, Mr.coffeeReally great solutions. I just add another [XmlArrayItem (ElementName = "MEMBERS", NestedLevel = 0)]. Works really great.

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.