473,325 Members | 2,805 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

How Do I Serialze Parent Hosting COLLECTION of Child objects

I've got a parent object that is a collection and it in turn is
hosting 2 child objects.
My searlization has worked perfectly to plan in as much that I wanted
to dictate the names of the objects and properties via XMLatrributes
with a prefix of "THE_.."....except for the name of the child objects.
This should say THE_NEW_Child but instead still persist in saying
"NewChild" (Child properties are as planned)

What's the solution?

Here's the code which is quite simple and compiles as 3 modules called
ConsolePogram.CS, New_Parent.CS and New_Child.CS:-
1/ ConsolePogram.CS:-

using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;

namespace Serialization
{
class Program
{
static void Main(string[] args)
{
// Create parent (collection) & child object
New_Parent p = new New_Parent();
NewChild c = new NewChild();

// Add 2 children to parent collection
c.A = "first instance of a";
c.B = "first instance of b";
p.Add( c);

c.A = "scond instance of a";
c.B = "scond instance of b";
p.Add (c);
// Serialize and display
System.Xml.Serialization.XmlSerializer x = new
System.Xml.Serialization.XmlSerializer(p.GetType() );
x.Serialize(Console.Out, p);

// Pause the screen
Console.ReadLine();
}
}
}
2/ New_Parent.CS:-
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;

namespace Serialization
{
[XmlRootAttribute("THE_NEW_Parent", Namespace = "", IsNullable =
false)]
public class New_Parent : List<NewChild>
{
}
}
3/ New_Child.CS:-
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;

namespace Serialization
{
//QUESTION - hOW DO I OVERRIDE NewChild class name because this
// following XML attribute doesn't do it!
[XmlRootAttribute("THE_NEW_Child", Namespace = "", IsNullable =
false)]
public class NewChild
{
private String a;

[XmlAttribute("THE_A")]
public String A
{
get { return a; }
set { a = value; }
}
private String b;

[XmlAttribute("THE_B")]
public String B
{
get { return b; }
set { b = value; }
}

}
}

May 8 '07 #1
1 2117

See
9/21/2005
XmlSerialization with IDictionary and CollectionBase Objects
http://sholliday.spaces.live.com/blog/

for some collection based tidbits.

Unforunately, I haven't had time to delve into Generics and Xml
serialization.



"DesperateDan" <da*********@assureweb.co.ukwrote in message
news:11*********************@n59g2000hsh.googlegro ups.com...
I've got a parent object that is a collection and it in turn is
hosting 2 child objects.
My searlization has worked perfectly to plan in as much that I wanted
to dictate the names of the objects and properties via XMLatrributes
with a prefix of "THE_.."....except for the name of the child objects.
This should say THE_NEW_Child but instead still persist in saying
"NewChild" (Child properties are as planned)

What's the solution?

Here's the code which is quite simple and compiles as 3 modules called
ConsolePogram.CS, New_Parent.CS and New_Child.CS:-
1/ ConsolePogram.CS:-

using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;

namespace Serialization
{
class Program
{
static void Main(string[] args)
{
// Create parent (collection) & child object
New_Parent p = new New_Parent();
NewChild c = new NewChild();

// Add 2 children to parent collection
c.A = "first instance of a";
c.B = "first instance of b";
p.Add( c);

c.A = "scond instance of a";
c.B = "scond instance of b";
p.Add (c);
// Serialize and display
System.Xml.Serialization.XmlSerializer x = new
System.Xml.Serialization.XmlSerializer(p.GetType() );
x.Serialize(Console.Out, p);

// Pause the screen
Console.ReadLine();
}
}
}
2/ New_Parent.CS:-
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;

namespace Serialization
{
[XmlRootAttribute("THE_NEW_Parent", Namespace = "", IsNullable =
false)]
public class New_Parent : List<NewChild>
{
}
}
3/ New_Child.CS:-
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;

namespace Serialization
{
//QUESTION - hOW DO I OVERRIDE NewChild class name because this
// following XML attribute doesn't do it!
[XmlRootAttribute("THE_NEW_Child", Namespace = "", IsNullable =
false)]
public class NewChild
{
private String a;

[XmlAttribute("THE_A")]
public String A
{
get { return a; }
set { a = value; }
}
private String b;

[XmlAttribute("THE_B")]
public String B
{
get { return b; }
set { b = value; }
}

}
}

May 8 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Danny Tuppeny | last post by:
Hi all, I've been trying to write some classes, so when I have a parent-child relationship, such as with Folders in my application, I don't have to remember to add a parent reference, as well as...
6
by: Edwinah63 | last post by:
Hi everyone, could someone give me some thoughts on the best way to manage mdi parent and child forms? in vb6 i could scroll through the forms collection and determine which forms were...
10
by: Charles Law | last post by:
For some reason, when I click the X to close my MDI parent form, the action appears to be re-directed to one of the MDI child forms, and the parent remains open. I am then unable to close the...
5
by: Noozer | last post by:
I have two Classes in use in an ASP application... The first Class is named "ORDER". It represents one job order. It has a Delete method, among others, which deletes it from my database. The...
10
by: Goran Djuranovic | last post by:
Hi all, Does anyone know how to declare a variable in a class to be accessible ONLY from a classes instantiated within that class? For example: ************* CODE ***************** Public...
2
by: epaetz | last post by:
Is there a way to decouple the linkage between a parent and a child window? Does the parent window have any sort of a collection that holds all the children that it has spawned? I want to...
6
by: reandeau | last post by:
I'm building out a OO based app in PHP 5 but I'm getting a little confused on children contructing parents. I have a parent that looks like this: abstract Class State { protected $database;...
6
by: Sashi | last post by:
class parent{ Parent(){}; ~Parent(){}; } Child: public Parent{ Child(){}; ~Child(){};
3
by: Maximiliano | last post by:
Hello, I have an asp.net project that calculates a general tax. Ok, this tax is a big object formed with another child objects (as a mather of fact 15 another child object within it), like Ship,...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.