C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,418 questions
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Hello Everyone,
I have below models and i need the return xml response like below. Here how to do this. please help me
public class School
{
public List<Student> StudentList;
public int SchoolID { get; set; }
public string SchoolName { get; set; }
}
public class Student
{
public string StudentName { get; set; }
public string ClassName { get; set; }
public int ? Age { get; set; }
}
Required Output
<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
Try something like this:
List<School> myschools = . . . the list of the schools
var xr = new XmlSerializer( typeof( List<School> ), new XmlRootAttribute { ElementName = "RESPONSE" } );
string xml;
using( var sw = new StringWriter( ) )
{
xr.Serialize( sw, myschools );
xml = sw.ToString( );
}
// display the result
Console.WriteLine( xml );