How Do I Serialze Parent Hosting COLLECTION of Child objects

D

DesperateDan

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; }
}

}
}
 

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