1

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:

  • Get the XML from the webservice
  • Parse each student
  • Store each student in the database
Fred Fickleberry III
  • 2,439
  • 4
  • 34
  • 50
  • You left out the details about WebService and Database APIs. That makes a detailed answer difficult. – H H Oct 31 '12 at 10:54
  • Could the students already exist in the Db? – H H Oct 31 '12 at 10:55
  • I basically have access to remote XML files; 1 for the parents, 1 for the students and 1 for the classes they take in school. I need to import the information periodically in the database. The XML is accessible via a http-call. The students will not be added manually, just via the import, so I can adjust the tables to suit the import if necessary -- I just don't know the correct way to approach this (coming from a RoR background) – Fred Fickleberry III Oct 31 '12 at 11:03
  • btw, I first have to import the parents, then import the students and LINK them to the existing parents via a parentID.. brr. – Fred Fickleberry III Oct 31 '12 at 11:04

3 Answers3

1

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.

user15741
  • 1,392
  • 16
  • 27
0

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.

Here's an example

Community
  • 1
  • 1
John Mc
  • 2,862
  • 1
  • 22
  • 37
0
  • XML Serialization to a StudentDTO object.
  • StudentDTO to Student mapping.
  • Mapped student to be saved in DB.

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.
    }   
}
NileshChauhan
  • 5,469
  • 2
  • 29
  • 43