Custom Object Structure

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>
 
M

Marc Gravell

You could try adding the [XmlIgnore] decoration to the Teacher's Students
property - this might help.

(its in the System.Xml.Serialization namespace)

Also - yes it will compile, but I would advise against "Class" as a class
name.

Marc
 
G

Guest

Marc:

Assuming that I did it correctly, the decoration did not appear to change
anything in the xml output.

So you think the structure of the custom object is OK, otherwise...nothing I
can do to make it write out the xml differently?

Thanks,

Paul

================================================
 
M

Marc Gravell

It works fine for me... are you sure you have rebuilt, and are you sure you
are putting the data in the right place? (I note you have students
collections at both levels) What framework are you using? 1.1? 2.0?

My (fairly random) test code (written to be broadly 1.1 compliant, but
tested only in 2.0) - the xml output looks just like you wanted to me
(having just added [XmlIgnore] to Teacher.Students):

[WebMethod]
public School.Teacher[] Test() {
ArrayList teachers = new ArrayList();
School.Teacher teacher = new School.Teacher("Marc");
for (int i = 0; i < 10; i++) {
teacher.Students.Add(new School.Student("Student " +
i.ToString()));
}
for (int i = 0; i < 5; i++) {
School.Class newClass = new School.Class("Class " +
i.ToString());
teacher.Classes.Add(newClass);
newClass.Students.Add(teacher.Students[3]);
newClass.Students.Add(teacher.Students[5]);
newClass.Students.Add(teacher.Students[9]);
}
teachers.Add(teacher);
return (School.Teacher[])teachers.ToArray(typeof(School.Teacher));
}
 
G

Guest

Marc:

Well, I did it wrong. Thank you for the example code. Your code works fine
for me.

I used this to view the xml output:
// Deserialize the content of the 'teachers' array to an XML file
XmlSerializer x = new XmlSerializer(typeof(School.Teacher[]));
TextWriter writer = new StreamWriter(MapPath("Teachers.xml"));
x.Serialize(writer,Test());

I'm using 2.0. I'll remove the students at the class level and try again.

My main trouble is that creating custom objects like this (and trying to use
them) is all new to me. Do you have any book or web site suggestions for me
that would be particulary helpful when it comes to figuring out the type of
problem that I'm working on here?

Thanks again,

Paul

=================================================
 
M

Marc Gravell

That's a bit of an open question, and I'm not sure I could come close to a
"full" answer - however, one immediate thought: you have gone to a lot of
trouble to create type safe collection classes - but since these are fairly
"normal" you could save a *lot* of time by using Collection<T> or List<T>
instead (it was your use of CollectionBase for this that made me assume you
were using 1.1). There's nothing *wrong* with CollectionBase : it just sucks
time ;-p

Marc
 
G

Guest

Marc:

If you are still out there...

I'm having a difficult time understanding this stuff. I'm trying to use
this custom object with the Profile object in ASP.NET 2.0...I see how you
built the custom object in your code below, but I haven't been able to get it
into the Profile object. I've tried various things, but I get casting
errors. Sorry to ask, but can you help me further?

Paul

PS. I'll take a look at the generics...eventually.
===============================================
 
M

Marc Gravell

Still here...

If you mean the System.Web.Profile.DefaultProfile object exposed through the
Profile property, then I'm afraid this isn't something I have used, so I
can't help without more info (and even then I wouldn't trust me!).

Have you looked at MSDN/MSDN2 for examples of using this?

Marc
 
G

Guest

Hi Marc,

I've got numerous projects where I've written working examples using
different built in collections (ArrayList, Hashtable and StringCollections)
and I've gotten data in and out of the database and populated numerous
controls, but I'm having a real brain cramp when it comes to custom objects
(mostly, I think, because this is my first ever custom object and I'm
disoriented, or something that "isn't good".)

With the built-in Complex datatypes I'd expect code, something like the
following, to work, but it doesn't seem to...

// Complex Data Type
<add
name="School"
type="System.Collections.ArrayList"
serializeAs="Xml"
allowAnonymous="true"/>

// Custom Object Type
<add
name="Teachers"
type="School.Teachers"
serializeAs="Xml"
allowAnonymous="true"/>

//Three examples of the custom object:
//First line - using the Custom object.
//Second line - using the Profile object.
// Add Teacher----------------------------------
School.Teacher teacher = new School.Teacher("Marc");
Profile.Teachers.Add(teacher);

// Add Student----------------------------------
teacher.Students.Add(new School.Student("Student"));
Profile.Teachers[0].Students.Add(new School.Student("Student"));

// Add Class----------------------------------
School.sClass newClass = new School.sClass("Class");
Profile.Teachers[0].Classes.Add(new School.sClass("Class"));

Anyway, thanks for the help...I'm sure it is helping me inch my way closer
to a solution.

Paul

=====================================================
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top