//Parse the message and add it to the fake database public static void OnServerRequest(string message) { RegistrationStudent student = RegistrationStudent.DeserializeFromRosterStudent(message); Console.WriteLine(student.ToString()); student.AddRecordToDB(); }
// Parse a supplied LDPT message body string and map the values to a new Registration Student public static RegistrationStudent DeserializeFromRosterStudent(string LDTPMessage) { RegistrationStudent student = new RegistrationStudent(); try { //Split the string into rows containing only the key value pair string[] keyValuePairs = LDTPMessage.Split('\n'); //Dictionary for storing the key and value pairs of the Dictionary <string, string> messageMap = new Dictionary <string, string>(); //Loop for (int i = 0; i < keyValuePairs.Length; i++) { //Split the pair into two values string[] pair = keyValuePairs[i].Split('\t'); messageMap.Add(pair[0], pair[1]); } //Set the relavent fields from the LDTP message if the correct type matches if (messageMap["type"] == "RosterStudent") { student.StudentID = int.Parse(messageMap["stuID"]); student.StudentName = messageMap["name"]; student.StudentSSN = messageMap["ssn"]; student.StudentEmail = messageMap["emailAddress"]; student.StudentPhone = messageMap["homePhone"]; } else { //Throw an error if the LDTP is not of the roster student type throw new Exception("LDTP message is not of type Roster Student"); } } catch (Exception e) { Console.WriteLine("Error deserializing student."); Console.WriteLine(e); } return(student); }