/// <summary> /// Generate XML to send database by using the student details in the datagridview /// </summary> /// <returns></returns> private string StudentDataSerializer() { MemoryStream msStudentDetails = null; try { string xmlStudDetails = null; //Create a List and add data to the list from DataGridview List <StudentDetailsXml> studentList = new List <StudentDetailsXml>(); for (int indexValue = 0; indexValue < dataGridStudent.Rows.Count; indexValue++) { if ((Convert.ToString(dataGridStudent.Rows[indexValue].Cells[0].Value)).Length != 0) { StudentDetailsXml stdXmlObj = new StudentDetailsXml(); stdXmlObj.studentId = Convert.ToInt32(dataGridStudent.Rows[indexValue].Cells[0].Value); stdXmlObj.studentName = Convert.ToString(dataGridStudent.Rows[indexValue].Cells[1].Value); stdXmlObj.dob = Convert.ToString(dataGridStudent.Rows[indexValue].Cells[2].Value); stdXmlObj.gradePointAvg = Convert.ToDouble(dataGridStudent.Rows[indexValue].Cells[3].Value); stdXmlObj.active = Convert.ToBoolean(dataGridStudent.Rows[indexValue].Cells[4].Value); studentList.Add(stdXmlObj); } else { } } //Serialize data which was added to the List by using a Memory Stream XmlSerializer serializerStudDetails = new XmlSerializer(typeof(List <StudentDetailsXml>)); msStudentDetails = new MemoryStream(); serializerStudDetails.Serialize(msStudentDetails, studentList); xmlStudDetails = System.Text.UTF8Encoding.UTF8.GetString(msStudentDetails.ToArray()); msStudentDetails.Close(); return(xmlStudDetails); } catch (Exception exception) { // Need to log exception to a file. Not Implemented throw new Exception(exception.Message); } finally { if (msStudentDetails != null) { msStudentDetails.Dispose(); } } }
/// <summary> /// Generate XML to send database by using the student details in the datagridview /// </summary> /// <returns></returns> private string StudentDataSerializer() { MemoryStream msStudentDetails = null; try { string xmlStudDetails = null; //Create a List and add data to the list from DataGridview List<StudentDetailsXml> studentList = new List<StudentDetailsXml>(); for (int indexValue = 0; indexValue < dataGridStudent.Rows.Count; indexValue++) { if ((Convert.ToString(dataGridStudent.Rows[indexValue].Cells[0].Value)).Length != 0) { StudentDetailsXml stdXmlObj = new StudentDetailsXml(); stdXmlObj.studentId = Convert.ToInt32(dataGridStudent.Rows[indexValue].Cells[0].Value); stdXmlObj.studentName = Convert.ToString(dataGridStudent.Rows[indexValue].Cells[1].Value); stdXmlObj.dob = Convert.ToString(dataGridStudent.Rows[indexValue].Cells[2].Value); stdXmlObj.gradePointAvg = Convert.ToDouble(dataGridStudent.Rows[indexValue].Cells[3].Value); stdXmlObj.active = Convert.ToBoolean(dataGridStudent.Rows[indexValue].Cells[4].Value); studentList.Add(stdXmlObj); } else { } } //Serialize data which was added to the List by using a Memory Stream XmlSerializer serializerStudDetails = new XmlSerializer(typeof(List<StudentDetailsXml>)); msStudentDetails = new MemoryStream(); serializerStudDetails.Serialize(msStudentDetails, studentList); xmlStudDetails = System.Text.UTF8Encoding.UTF8.GetString(msStudentDetails.ToArray()); msStudentDetails.Close(); return xmlStudDetails; } catch (Exception exception) { // Need to log exception to a file. Not Implemented throw new Exception(exception.Message); } finally { if (msStudentDetails != null) msStudentDetails.Dispose(); } }