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.Teacher s.Add(new School.Teacher( TextBox_Add_Tea cher.Text,
Profile.Classes ));
=============== =============== =============== =============== Custom Object
using System;
using System.Collecti ons;
namespace School
{
#region Student
Object+++++++++ +++++++++++++++ +++++++++++++++ +++++++++++++++ ++
[Serializable()]
public class Student
{
#region Private members (Fields) +++++++++++++++ +++++
int id;
string firstName;
string lastName;
int gPA;
int currentGradeLev el;
#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 CurrentGradeLev el
{
get { return this.currentGra deLevel; }
set { this.currentGra deLevel = 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
CurrentGradeLev el)
{
this.id = Id;
this.firstName = FirstName;
this.lastName = LastName;
this.gPA = GPA;
this.currentGra deLevel = CurrentGradeLev el;
}
#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(va lue);
}
public void Insert(int index, Student value)
{
List.Insert(ind ex, value);
}
public void Remove(Student value)
{
List.Remove(val ue);
}
public bool Contains(Studen t value)
{
return List.Contains(v alue);
}
#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,DateT ime 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(va lue);
}
public void Insert(int index, Class value)
{
List.Insert(ind ex, value);
}
public void Remove(Class value)
{
List.Remove(val ue);
}
public bool Contains(Class value)
{
return List.Contains(v alue);
}
#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(va lue);
}
public void Insert(int index, Teacher value)
{
List.Insert(ind ex, value);
}
public void Remove(Teacher value)
{
List.Remove(val ue);
}
public bool Contains(Teache r value)
{
return List.Contains(v alue);
}
#endregion -------------------------------------------
#region Constructors +++++++++++++++ +++++++++++++++ +++
public Teachers() { }
#endregion -------------------------------------------
}
}
=============== =============== =============== =============== Resulting xml in
Profile Object
<?xml version="1.0" encoding="utf-16"?>
<ArrayOfTeach er 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> 8 2020
You could try adding the [XmlIgnore] decoration to the Teacher's Students
property - this might help.
(its in the System.Xml.Seri alization namespace)
Also - yes it will compile, but I would advise against "Class" as a class
name.
Marc
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...not hing I
can do to make it write out the xml differently?
Thanks,
Paul
=============== =============== =============== ===
"Marc Gravell" wrote: You could try adding the [XmlIgnore] decoration to the Teacher's Students property - this might help.
(its in the System.Xml.Seri alization namespace)
Also - yes it will compile, but I would advise against "Class" as a class name.
Marc
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.Student s):
[WebMethod]
public School.Teacher[] Test() {
ArrayList teachers = new ArrayList();
School.Teacher teacher = new School.Teacher( "Marc");
for (int i = 0; i < 10; i++) {
teacher.Student s.Add(new School.Student( "Student " +
i.ToString()));
}
for (int i = 0; i < 5; i++) {
School.Class newClass = new School.Class("C lass " +
i.ToString());
teacher.Classes .Add(newClass);
newClass.Studen ts.Add(teacher. Students[3]);
newClass.Studen ts.Add(teacher. Students[5]);
newClass.Studen ts.Add(teacher. Students[9]);
}
teachers.Add(te acher);
return (School.Teacher[])teachers.ToArr ay(typeof(Schoo l.Teacher));
}
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(t ypeof(School.Te acher[]));
TextWriter writer = new StreamWriter(Ma pPath("Teachers .xml"));
x.Serialize(wri ter,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
=============== =============== =============== ====
"Marc Gravell" wrote: 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.Student s):
[WebMethod] public School.Teacher[] Test() { ArrayList teachers = new ArrayList(); School.Teacher teacher = new School.Teacher( "Marc"); for (int i = 0; i < 10; i++) { teacher.Student s.Add(new School.Student( "Student " + i.ToString())); } for (int i = 0; i < 5; i++) { School.Class newClass = new School.Class("C lass " + i.ToString()); teacher.Classes .Add(newClass); newClass.Studen ts.Add(teacher. Students[3]); newClass.Studen ts.Add(teacher. Students[5]); newClass.Studen ts.Add(teacher. Students[9]); } teachers.Add(te acher); return (School.Teacher[])teachers.ToArr ay(typeof(Schoo l.Teacher)); }
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
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...even tually.
=============== =============== =============== ==
"Marc Gravell" wrote: 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.Student s):
[WebMethod] public School.Teacher[] Test() { ArrayList teachers = new ArrayList(); School.Teacher teacher = new School.Teacher( "Marc"); for (int i = 0; i < 10; i++) { teacher.Student s.Add(new School.Student( "Student " + i.ToString())); } for (int i = 0; i < 5; i++) { School.Class newClass = new School.Class("C lass " + i.ToString()); teacher.Classes .Add(newClass); newClass.Studen ts.Add(teacher. Students[3]); newClass.Studen ts.Add(teacher. Students[5]); newClass.Studen ts.Add(teacher. Students[9]); } teachers.Add(te acher); return (School.Teacher[])teachers.ToArr ay(typeof(Schoo l.Teacher)); }
Still here...
If you mean the System.Web.Prof ile.DefaultProf ile 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
Hi Marc,
I've got numerous projects where I've written working examples using
different built in collections (ArrayList, Hashtable and StringCollectio ns)
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.Co llections.Array List"
serializeAs="Xm l"
allowAnonymous= "true"/>
// Custom Object Type
<add
name="Teachers"
type="School.Te achers"
serializeAs="Xm l"
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.Teacher s.Add(teacher);
// Add Student----------------------------------
teacher.Student s.Add(new School.Student( "Student")) ;
Profile.Teacher s[0].Students.Add(n ew School.Student( "Student")) ;
// Add Class----------------------------------
School.sClass newClass = new School.sClass(" Class");
Profile.Teacher s[0].Classes.Add(ne w School.sClass(" Class"));
Anyway, thanks for the help...I'm sure it is helping me inch my way closer
to a solution.
Paul
=============== =============== =============== ========
"Marc Gravell" wrote: Still here...
If you mean the System.Web.Prof ile.DefaultProf ile 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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Suzanne |
last post by:
Hi all,
I'm reposting this message as I'm experiencing this problem more and
more frequently :
I really hope someone out there can help me as I've been tearing my
hair out on this one for a good while and I'm getting really
frustrated now!
My problem is this - my custom controls periodically disappear from my
|
by: mtv |
last post by:
Hi all,
I have the following code:
================================
Webservice side:
public class MyWS: WebService
{
private myLib.DataObject curDataObject;
|
by: |
last post by:
Hi all, I have a question on reflection
Lets say I have a custom object called Address. Now, lets say I have a
string variable that holds the name of a variable in the object such as
"State.StateCode". How can I reflect upon the Address object so as to echo
the value contained in the string variable. Something like:-
Dim add as new Address
add.State.StateCode = "TX"
Dim str as string = "State.StateCode"
|
by: Techno_Dex |
last post by:
Has anyone come up with a slick way to make Custom Serializable Objects to
behave like DataSets when using WebServices? What I'm looking for is some
way to force the WSDL generated code to create a reference to my custom
object's namespace instead of creating it's own copy of the object in it's
own WS namespace. Either that, or some easy way to move all of the
properties from the Custom Serializable Object into the WSDL generated
object...
|
by: Simon Woods |
last post by:
Hi
Does anyone know if there any dotnet software out there which converts a
dataset into a collection of custom objects, programmatically i.e.
actually creates the class structure and collection class on-the-fly
based on the fields in the dataset and then creates and populates the
classes and adds them into the collection.
Thanks
| |
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
| |
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |