1

I want to produce the following xml output

<account>
    <id>123456</id>
    <roles>
        <role master-name="I80">
            <id>88</id>
            <unit>Implementer</unit>
        </role>
        <role master-name="S20">
            <id>21</id>
            <unit>Support</unit>
        </role>
    </roles>
</account>

So I create the following class

[XmlRoot("account")]
public class Account
{
    [XmlElement("id")]
    public int ID { get; set; }
    [XmlElement("roles")]
    public RolesList Roles { get; set; }
}

public class RolesList
{
    [XmlElement("role")]
    public List<Role> roles { get; set; }
}

public class Role
{
    [XmlAttribute("master-name")]
    public string MasterName { get; set; }
    [XmlElement("id")]
    public int ID { get; set; }
    [XmlElement("unit")]
    public string Unit { get; set; }
}

My question is, Is there a way to represent this any better? I want to avoid creating the RolesList class.

1 Answer 1

2

You should declare the list of roles like this in Account:

[XmlArray("roles")]
[XmlArrayItem("role")]
public List<Role> Roles { get; set; }
Sign up to request clarification or add additional context in comments.

2 Comments

I will try that, but I think XArrayItem will produce the element <Role> instead of <role> (lowercase)
@KishoreMasand: have you tried it out? I had tested it, and it seemed to work correctly.

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.