Abhijeet
Abhijeet

Reputation: 13856

Serialize Dotnet Collection without Root Element?

Consider following serialized xml:

<Course>
  <Title>Engineering</Title>
  <Student>
    <Name>John Doe</Name>
  </Student>
  <Student>
    <Name>Jane Doe</Name>
  </Student>
   ...
</Course>

Unfortunately I am not in a position to modify schema. (Ideally I should have wrapped Student class into Students root element!)

How do I define entity classes so that serialization could work properly?

I tried following code, but this generates xml with Students as wrapper element.

public class Course
{
    public string Title { get; set; }
    public List<Student> Students { get; set; }
}

public class Student
{
    public string Name { get; set; }
}

Upvotes: 1

Views: 485

Answers (1)

Fung
Fung

Reputation: 3558

Add an XmlElement attribute to your Students list, like this:

public class Course
{
    public string Title { get; set; }

    [XmlElement("Student")]
    public List<Student> Students { get; set; }
}

Upvotes: 2

Related Questions