G
Guest
I'm trying to save data from a custom object into the profile object, but it
is not structured the way that I want.
I'm trying to get the custom object to serialize as xml to a Profile object
like so:
<Teachers>
<Teacher>
<Classes>
<Class>
<Students>
<Student>
I have the nesting correct down through Class, but I haven't been able to
get the Students and Student to nest properly...they actually nest under
Teacher, the same as the Classes level.
There are three files below: 1) aspx code, 2) custom object, 3) the xml
output to the Profile table in the ASPNETdb.
I suppose that the custom object is not written correctly to attain the
desired nesting ...or is it something else?
============================================================ Code in aspx page
Profile.Teachers.Add(new School.Teacher(TextBox_Add_Teacher.Text,
Profile.Classes));
============================================================ Custom Object
using System;
using System.Collections;
namespace School
{
#region Student
Object++++++++++++++++++++++++++++++++++++++++++++++++++++++++
[Serializable()]
public class Student
{
#region Private members (Fields) ++++++++++++++++++++
int id;
string firstName;
string lastName;
int gPA;
int currentGradeLevel;
#endregion -------------------------------------------
#region Public Accessors (Properties) ++++++++++++++
public int Id
{
get { return this.id; }
set { this.id = value; }
}
public string FirstName
{
get { return this.firstName; }
set { this.firstName = value; }
}
public string LastName
{
get { return this.lastName; }
set { this.lastName = value; }
}
public int GPA
{
get { return this.gPA; }
set { this.gPA = value; }
}
public int CurrentGradeLevel
{
get { return this.currentGradeLevel; }
set { this.currentGradeLevel = value; }
}
#endregion -------------------------------------------
#region Constructors +++++++++++++++++++++++++++++++++
// Empty constructor
public Student()
{ }
// Partial Constructor
public Student(string FirstName)
{
this.firstName = FirstName;
}
// Full Constructor
public Student(int Id, string FirstName, string LastName, int GPA, int
CurrentGradeLevel)
{
this.id = Id;
this.firstName = FirstName;
this.lastName = LastName;
this.gPA = GPA;
this.currentGradeLevel = CurrentGradeLevel;
}
#endregion -------------------------------------------
}
#endregion
[Serializable()]
public class Students : CollectionBase
{
#region Public Accessors (Properties) ++++++++++++++
public Student this[int index]
{
set
{
List[index] = value;
}
get
{
return (Student)List[index];
}
}
public int Add(Student value)
{
return List.Add(value);
}
public int IndexOf(Student value)
{
return List.IndexOf(value);
}
public void Insert(int index, Student value)
{
List.Insert(index, value);
}
public void Remove(Student value)
{
List.Remove(value);
}
public bool Contains(Student value)
{
return List.Contains(value);
}
#endregion -------------------------------------------
#region Constructors +++++++++++++++++++++++++++++++++
public Students()
{ }
#endregion -------------------------------------------
}
#region Class
Object++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
[Serializable()]
public class Class
{
#region Private members (Fields) ++++++++++++++++++++
private string className;
private DateTime startTime;
private DateTime endTime;
School.Students students = new School.Students();
#endregion -------------------------------------------
#region Public Accessors (Properties) ++++++++++++++
public string Name
{
get { return className; }
set { className = value; }
}
public DateTime StartTime
{
get { return this.startTime; }
set { this.startTime = value; }
}
public DateTime EndTime
{
get { return this.endTime; }
set { this.endTime = value; }
}
public School.Students Students
{
get { return this.students; }
set { this.students = value; }
}
#endregion -------------------------------------------
#region Constructors +++++++++++++++++++++++++++++++++
// empty constructor
public Class()
{ }
// Partial Constructor1
public Class(string ClassName)
{
this.className = ClassName;
}
// Partial Constructor2
public Class(string ClassName, School.Students Students)
{
this.className = ClassName;
this.students = Students;
}
// Full Constructor
public Class(string ClassName,DateTime StartTime, DateTime EndTime,
School.Students Students)
{
this.className = ClassName;
this.startTime = StartTime;
this.endTime = EndTime;
this.students = Students;
}
#endregion -------------------------------------------
}
#endregion
[Serializable()]
public class Classes : CollectionBase
{
public Class this[int index]
{
set
{
List[index] = value;
}
get
{
return (Class)List[index];
}
}
#region Public Accessors (Properties) ++++++++++++++
public int Add(Class value)
{
return List.Add(value);
}
public int IndexOf(Class value)
{
return List.IndexOf(value);
}
public void Insert(int index, Class value)
{
List.Insert(index, value);
}
public void Remove(Class value)
{
List.Remove(value);
}
public bool Contains(Class value)
{
return List.Contains(value);
}
#endregion -------------------------------------------
#region Constructors +++++++++++++++++++++++++++++++++
public Classes()
{ }
#endregion -------------------------------------------
}
#region Teacher
Object++++++++++++++++++++++++++++++++++++++++++++++++++++++++
[Serializable()]
public class Teacher
{
#region Private members (Fields) ++++++++++++++++++++
private string name;
School.Classes classes = new School.Classes();
School.Students students = new School.Students();
#endregion -------------------------------------------
#region Public Accessors (Properties) ++++++++++++++
public string Name
{
get { return name; }
set { name = value; }
}
public School.Classes Classes
{
get { return this.classes; }
set { this.classes = value; }
}
public School.Students Students
{
get { return this.students; }
set { this.students = value; }
}
#endregion -------------------------------------------
#region Constructors +++++++++++++++++++++++++++++++++
public Teacher()
{ }
public Teacher(string name)
{
this.name = Name;
}
public Teacher(string Name, School.Classes Classes)
{
this.name = Name;
this.classes = Classes;
}
public Teacher(string Name, School.Classes Classes, School.Students Students)
{
this.name = Name;
this.classes = Classes;
this.students = Students;
}
#endregion -------------------------------------------
}
#endregion
[Serializable()]
public class Teachers : CollectionBase
{
#region Private members (Fields) +++++++++++++++
#endregion --------------------------------------
#region Indexer +++++++++++++++++++++++++++++++++
public Teacher this[int index]
{
set
{
List[index] = value;
}
get
{
return (Teacher)List[index];
}
}
#endregion ------------------------------------
#region Public Accessors (Properties) ++++++++++++++
public int Add(Teacher value)
{
return List.Add(value);
}
public int IndexOf(Teacher value)
{
return List.IndexOf(value);
}
public void Insert(int index, Teacher value)
{
List.Insert(index, value);
}
public void Remove(Teacher value)
{
List.Remove(value);
}
public bool Contains(Teacher value)
{
return List.Contains(value);
}
#endregion -------------------------------------------
#region Constructors +++++++++++++++++++++++++++++++++
public Teachers() { }
#endregion -------------------------------------------
}
}
============================================================Resulting xml in
Profile Object
<?xml version="1.0" encoding="utf-16"?>
<ArrayOfTeacher xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Teacher>
<Name>Willy</Name>
<Classes>
<Class>
<Name>Biochem</Name>
<Students />
</Class>
</Classes>
<Students>
<Student>
<Id>0</Id>
<FirstName>Abby</FirstName>
<GPA>0</GPA>
</Student>
<Student>
<Id>0</Id>
<FirstName>Fred</FirstName>
<GPA>0</GPA>
</Student>
</Students>
</Teacher>
</ArrayOfTeacher>
is not structured the way that I want.
I'm trying to get the custom object to serialize as xml to a Profile object
like so:
<Teachers>
<Teacher>
<Classes>
<Class>
<Students>
<Student>
I have the nesting correct down through Class, but I haven't been able to
get the Students and Student to nest properly...they actually nest under
Teacher, the same as the Classes level.
There are three files below: 1) aspx code, 2) custom object, 3) the xml
output to the Profile table in the ASPNETdb.
I suppose that the custom object is not written correctly to attain the
desired nesting ...or is it something else?
============================================================ Code in aspx page
Profile.Teachers.Add(new School.Teacher(TextBox_Add_Teacher.Text,
Profile.Classes));
============================================================ Custom Object
using System;
using System.Collections;
namespace School
{
#region Student
Object++++++++++++++++++++++++++++++++++++++++++++++++++++++++
[Serializable()]
public class Student
{
#region Private members (Fields) ++++++++++++++++++++
int id;
string firstName;
string lastName;
int gPA;
int currentGradeLevel;
#endregion -------------------------------------------
#region Public Accessors (Properties) ++++++++++++++
public int Id
{
get { return this.id; }
set { this.id = value; }
}
public string FirstName
{
get { return this.firstName; }
set { this.firstName = value; }
}
public string LastName
{
get { return this.lastName; }
set { this.lastName = value; }
}
public int GPA
{
get { return this.gPA; }
set { this.gPA = value; }
}
public int CurrentGradeLevel
{
get { return this.currentGradeLevel; }
set { this.currentGradeLevel = value; }
}
#endregion -------------------------------------------
#region Constructors +++++++++++++++++++++++++++++++++
// Empty constructor
public Student()
{ }
// Partial Constructor
public Student(string FirstName)
{
this.firstName = FirstName;
}
// Full Constructor
public Student(int Id, string FirstName, string LastName, int GPA, int
CurrentGradeLevel)
{
this.id = Id;
this.firstName = FirstName;
this.lastName = LastName;
this.gPA = GPA;
this.currentGradeLevel = CurrentGradeLevel;
}
#endregion -------------------------------------------
}
#endregion
[Serializable()]
public class Students : CollectionBase
{
#region Public Accessors (Properties) ++++++++++++++
public Student this[int index]
{
set
{
List[index] = value;
}
get
{
return (Student)List[index];
}
}
public int Add(Student value)
{
return List.Add(value);
}
public int IndexOf(Student value)
{
return List.IndexOf(value);
}
public void Insert(int index, Student value)
{
List.Insert(index, value);
}
public void Remove(Student value)
{
List.Remove(value);
}
public bool Contains(Student value)
{
return List.Contains(value);
}
#endregion -------------------------------------------
#region Constructors +++++++++++++++++++++++++++++++++
public Students()
{ }
#endregion -------------------------------------------
}
#region Class
Object++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
[Serializable()]
public class Class
{
#region Private members (Fields) ++++++++++++++++++++
private string className;
private DateTime startTime;
private DateTime endTime;
School.Students students = new School.Students();
#endregion -------------------------------------------
#region Public Accessors (Properties) ++++++++++++++
public string Name
{
get { return className; }
set { className = value; }
}
public DateTime StartTime
{
get { return this.startTime; }
set { this.startTime = value; }
}
public DateTime EndTime
{
get { return this.endTime; }
set { this.endTime = value; }
}
public School.Students Students
{
get { return this.students; }
set { this.students = value; }
}
#endregion -------------------------------------------
#region Constructors +++++++++++++++++++++++++++++++++
// empty constructor
public Class()
{ }
// Partial Constructor1
public Class(string ClassName)
{
this.className = ClassName;
}
// Partial Constructor2
public Class(string ClassName, School.Students Students)
{
this.className = ClassName;
this.students = Students;
}
// Full Constructor
public Class(string ClassName,DateTime StartTime, DateTime EndTime,
School.Students Students)
{
this.className = ClassName;
this.startTime = StartTime;
this.endTime = EndTime;
this.students = Students;
}
#endregion -------------------------------------------
}
#endregion
[Serializable()]
public class Classes : CollectionBase
{
public Class this[int index]
{
set
{
List[index] = value;
}
get
{
return (Class)List[index];
}
}
#region Public Accessors (Properties) ++++++++++++++
public int Add(Class value)
{
return List.Add(value);
}
public int IndexOf(Class value)
{
return List.IndexOf(value);
}
public void Insert(int index, Class value)
{
List.Insert(index, value);
}
public void Remove(Class value)
{
List.Remove(value);
}
public bool Contains(Class value)
{
return List.Contains(value);
}
#endregion -------------------------------------------
#region Constructors +++++++++++++++++++++++++++++++++
public Classes()
{ }
#endregion -------------------------------------------
}
#region Teacher
Object++++++++++++++++++++++++++++++++++++++++++++++++++++++++
[Serializable()]
public class Teacher
{
#region Private members (Fields) ++++++++++++++++++++
private string name;
School.Classes classes = new School.Classes();
School.Students students = new School.Students();
#endregion -------------------------------------------
#region Public Accessors (Properties) ++++++++++++++
public string Name
{
get { return name; }
set { name = value; }
}
public School.Classes Classes
{
get { return this.classes; }
set { this.classes = value; }
}
public School.Students Students
{
get { return this.students; }
set { this.students = value; }
}
#endregion -------------------------------------------
#region Constructors +++++++++++++++++++++++++++++++++
public Teacher()
{ }
public Teacher(string name)
{
this.name = Name;
}
public Teacher(string Name, School.Classes Classes)
{
this.name = Name;
this.classes = Classes;
}
public Teacher(string Name, School.Classes Classes, School.Students Students)
{
this.name = Name;
this.classes = Classes;
this.students = Students;
}
#endregion -------------------------------------------
}
#endregion
[Serializable()]
public class Teachers : CollectionBase
{
#region Private members (Fields) +++++++++++++++
#endregion --------------------------------------
#region Indexer +++++++++++++++++++++++++++++++++
public Teacher this[int index]
{
set
{
List[index] = value;
}
get
{
return (Teacher)List[index];
}
}
#endregion ------------------------------------
#region Public Accessors (Properties) ++++++++++++++
public int Add(Teacher value)
{
return List.Add(value);
}
public int IndexOf(Teacher value)
{
return List.IndexOf(value);
}
public void Insert(int index, Teacher value)
{
List.Insert(index, value);
}
public void Remove(Teacher value)
{
List.Remove(value);
}
public bool Contains(Teacher value)
{
return List.Contains(value);
}
#endregion -------------------------------------------
#region Constructors +++++++++++++++++++++++++++++++++
public Teachers() { }
#endregion -------------------------------------------
}
}
============================================================Resulting xml in
Profile Object
<?xml version="1.0" encoding="utf-16"?>
<ArrayOfTeacher xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Teacher>
<Name>Willy</Name>
<Classes>
<Class>
<Name>Biochem</Name>
<Students />
</Class>
</Classes>
<Students>
<Student>
<Id>0</Id>
<FirstName>Abby</FirstName>
<GPA>0</GPA>
</Student>
<Student>
<Id>0</Id>
<FirstName>Fred</FirstName>
<GPA>0</GPA>
</Student>
</Students>
</Teacher>
</ArrayOfTeacher>