Reputation: 2519
I'm calling a webservice, which returns XML with students. I need to store all students in my database (existing student table).
Right now I have this XMLStudentParser class that gets the XML, but I'm lost at how to proceed in storing each student record to the database. Do I use the XMLReader to loop through the students and add each student to a List<Student>
, and then save that list to the database?
Remote XML via webservice.
<Response>
<Result>True</Result>
<Table>
<Students>
<Student>
<StudentID>14165</StudentID>
<StudentName>Jeff Smith</StudentName>
<GroupId>9109</GroupId>
</Student>
<Student>
<StudentID>14168</StudentID>
<StudentName>Mary Jones</StudentName>
<GroupId>9109</GroupId>
</Student>
</Students>
</Table>
</Response>
My Student model
public class Student
{
public int StudentId { get; set; }
public string FullName { get; set; }
public int GrpId { get; set; }
}
How would the best practice code look to:
Upvotes: 1
Views: 1047
Reputation: 1412
As previous answers have stated, making your object definition match the XML by adding XML Element attributes is probably the easiest way to deserialize the XML:
[XmlElement]
public class Response
{
[XmlElement]
bool Result;
public tbl[] Table;
}
public class tbl
{
public Student[] Students;
}
public class Student
{
public int StudentId { get; set; }
public string FullName { get; set; }
[XmlElement(ElementName = "GroupId")]
public int GrpId { get; set; }
}
You can then insert these values into a staging table for Students and Parents and use SQL MERGE statements to update your main data tables.
Upvotes: 1
Reputation: 5569
The StudentDTO is recommended here as you want to decouple Student class from the XML definition from web. If at all the XML definition changes you would only have to change DTO and not the Student implementation.
Potential non production code:
[XmlElement]
public class StudentDTO
{
[XmlElement]
public string StudentName {get;set;}
}
[XmlElement]
public class StudentsDTO : List<StudentDTO>
{
}
public class Student
{
public string Name {get;set;}
}
//Ideally on big System, Mapper class would be a generic on the lines of Mapper<Source,Target>
//Mapper<StudentDTO,Student> and based on some rules it would do mapping.
public class StudentDTOToStudentMapper
{
public Student GetStudentForDTO(StudentDTO dto)
{
//create object of student
// Map corresponding property of StudentDTO to Student
// e.g. StudentName to Name
}
}
public class Client
{
public static void Main(DBHelper dbHandler, XMLSerialiser seriliaser, WebService serviceToCall,StudentDTOToStudentMapper mapper )
{
// XmlDocument/Object obj = serviceToCall.GetStudentsXML();
// StudentsDTO students = Seriliaser.Deserialise(XML);
// IEnumerable<Student> studentObjects = from eachDTO in students
// select mapper.GetStudentForDTO(eachDTO)
// bool IsSaved = dbHandler.Save(students);
// Based on IsSaved show the status.
}
}
Upvotes: 0
Reputation: 2933
If you could change your student class properties to match the Xml element names and/or decorate the properties with attributes indicating what XML value goes to what class property, then you could use the .Net to deserialise the XML into a List Students in one line.
Then just persist to the DB as you normally would.
Upvotes: 0