By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,589 Members | 1,194 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,589 IT Pros & Developers. It's quick & easy.

How Do I Serialze Parent Hosting COLLECTION of Child objects

P: n/a
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
Share this Question
Share on Google+
1 Reply


P: n/a

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 discussion thread is closed

Replies have been disabled for this discussion.