Question How to serialize to xml return type

chandu40

New member
Joined
Apr 22, 2021
Messages
4
Programming Experience
1-3
Hello Everyone,

I have below models and i need the return xml response like below. Here how to do this. please help me
C#:
public class School
{       
    public List<Student> StudentList;       
    public int? SchoolID { get; set; }     
    public int? SchoolName { get; set; }
}

public class Student
{
    public string StudentName { get; set; }
    public string ClassName { get; set; }
    public string Age { get; set; }         
}
Output
XML:
<RESPONSE>
    <School>
        <SchoolID>102</SchoolID>
        <SchoolName>ABC</SchoolName>
        <StudentList>
            <Student>
                <StudentName>JOHN</StudentName>
                <ClassName>IX</ClassName>
                <Age>13</Age>
            </Student>
            <Student>
                <StudentName>Taylor</StudentName>
                <ClassName>IIX</ClassName>
                <Age>11</Age>
            </Student>
        </StudentList>
    </School>
    <School>
        <SchoolID>103</SchoolID>
        <SchoolName>BCD</SchoolName>
        <StudentList>
            <Student>
                <StudentName>CHRIS</StudentName>
                <ClassName>IV</ClassName>
                <Age>9</Age>
            </Student>
            <Student>
        </StudentList>
    </School>
</RESPONSE>

Thanks,
King
 
Last edited by a moderator:
Please us the Inline code button only for inline code. For sizable code snippets, use the Code button.

Also, you should be aware that, in your School class, StudentList is a field when it ought to be a read-only property.

Also, the more appropriate name would Students. Notice that the .NET Framework already has Control.Controls, DataSet.Tables, DataTable.Rows, ListView.Items, etc. Convention is to pluralise the name, not suffix it with "List" or the like.
 
As for the issue, what have you tried and where are you stuck? There's loads of information out there about writing XML or serialising objects to XML. It's not really for us to simply repeat what's already available. You should research the topic, make your best attempt to implement what you have learned and then, if you encounter an issue, post here about that. We can then help you fix the problem, rather than just doing the work for you.
 
yes. here everything is string. so that i can convert later after deserializng.
That doesn't really follow. If the data represents a number then it should be a numeric data type. It can still be serialised to XML either way but numeric data should always be a numeric type.
I have already "School" list i just need to send this list as param to get required xml output
So do so then. If you encounter an actual issue, then show us what you did and tell us what happened.
 
yes. here everything is string. so that i can convert later after deserializng.
I'm talking about the data types in your C# classes, not the XML data. I suspect that you have actually transposed two of your data types because you have this:
C#:
public int? SchoolName { get; set; }
which obviously doesn't make sense, and this:
C#:
public string Age { get; set; }
which isn't quite as bad but is still bad. I suspect that they should be the other way, i.e.
C#:
public string SchoolName { get; set; }
and:
C#:
public int? Age { get; set; }
 
ohh sorry. it was my bad here
public string SchoolName { get; set; }
public int? Age { get; set; }

above are correct one
 
Back
Top Bottom