0

In .NET 5 I setup a webserver with OWIN

public void ConfigureServices(IServiceCollection services)
{
    services.AddControllers(options =>
    {
        options.RespectBrowserAcceptHeader = true;
    }).AddXmlSerializerFormatters();
}

I have a Model PresEvents with a collection Events

public class PersEvents
{
    public List<Event> Events { get; } = new List<Event>();
}

public class Event
{
    public string Sn { get; set; } = string.Empty;
    public string Loiname { get; set; } = string.Empty;
}

I have a controller returns this PersEvents instance

[HttpGet]
public PersEvents GetEventsData()
{
    PersEvents events = new PersEvents();
    events.Events.Add(new Event { Sn = "123", Loiname = "Ohio" });
    events.Events.Add(new Event { Sn = "456", Loiname = "Ohio" });
    return events;
}

When I call GetEventsData I can get the XML formatted data like below

<PersEvents xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <Events>
        <Event>
            <Sn>123</Sn>
            <Loiname>Ohio</Loiname>
        </Event>
        <Event>
            <Sn>456</Sn>
            <Loiname>Ohio</Loiname>
        </Event>
    </Events>
</PersEvents>

the question is I don't want the Events attribute, I just want all Event segments after PresEvents, How can I do that?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

2 Answers2

0

Make PerseEvents a collection type, then map all the inherited members to this.Events.

public class PersEvents : ICollection<Event>
{
    public List<Event> Events { get; } = new List<Event>();
    public IEnumerator GetEnumerator() => this.Events.GetEnumerator();
}
Michael Coxon
  • 5,311
  • 1
  • 24
  • 51
0

Finally I noticed add an attribute to the Events property can remove the container attribute.

[XmlElement("Event")]
public List<Event> Events { get; set; } = new List<Event>();