0

I need a resulting XML like this:

<ListOfStudents>
    <ClassOfStudents value="1">
        <Student first="asf" last="asf">
        <Student first="asf" last="asf">
        ...
    </ClassOfStudents>
    <ClassOfStudents value="2">
        <Student first="asf" last="asf">
        <Student first="asf" last="asf">
        ...
    </ClassOfStudents>
    <ClassOfStudents value="3">
        <Student first="asf" last="asf">
        <Student first="asf" last="asf">
        ...
    </ClassOfStudents>
    <ClassOfStudents value="4">
        <Student first="asf" last="asf">
        <Student first="asf" last="asf">
        ...
    </ClassOfStudents>
    <ClassOfStudents value="5">
        <Student first="asf" last="asf">
        <Student first="asf" last="asf">
        ...
    </ClassOfStudents>
</ListOfStudents>

Till now I have come up with this. But the problem is I can’t figure out how to include types in it?

public class ListOfStudents
{
    public ClassOfStudents _ClassOfStudents;
}

public class ClassOfStudents    
{
    public string Student;  
}

The value of types value, first, last would be assigned at the run-time but how should I model my classes to achieve the above XML after serialization?

Maven
  • 14,587
  • 42
  • 113
  • 174

2 Answers2

1

You just need to make the Student have two properties maybe as string for first and last, the ClassOfStudents should hold a list of Studnets and an id, and a ListOfStudents shoudl have a list of ClassOfStudents, that's all!

public class ListOfStudents
{
    public List<ClassOfStudents> ClassOfStudents;
}

public class ClassOfStudents    
{
    public int value;  
    public List<Student> Students;  
}

public class Student
{
    public string first;  
    public string last; 
}

And to deserialize into ListOfStudents see this topic.

Community
  • 1
  • 1
meJustAndrew
  • 6,011
  • 8
  • 50
  • 76
0

I create model classes for achieving your xml . I give you smiple way of create class file as per xml . 1. Create class file . 2.Copy xml content . 3. paste into class file as below snap .

You will get following result class file .

enter image description here

[System.SerializableAttribute()]
public partial class ListOfStudents
{
    [System.Xml.Serialization.XmlElementAttribute("ClassOfStudents")]
    public ListOfStudentsClassOfStudents[] ClassOfStudents { get; set; }
}


/// <remarks/>
[System.SerializableAttribute()]
public partial class ListOfStudentsClassOfStudents
{
     [System.Xml.Serialization.XmlElementAttribute("Student")]
    public ListOfStudentsClassOfStudentsStudent[] Student { get; set; }
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public byte value { get; set; }

}

/// <remarks/>
[System.SerializableAttribute()]
public partial class ListOfStudentsClassOfStudentsStudent
{
     [System.Xml.Serialization.XmlAttributeAttribute()]
    public string first { get; set; }
     [System.Xml.Serialization.XmlAttributeAttribute()]
    public string last { get; set; }

}

It will work as per your xml .

Thanks .

Ronak Patel
  • 630
  • 4
  • 15