Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / May 2007

Tip: Looking for answers? Try searching our database.

How Do I Serialze Parent Hosting COLLECTION of Child objects

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
DesperateDan - 08 May 2007 21:46 GMT
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; }
       }

   }
}
sloan - 08 May 2007 22:04 GMT
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.

> I've got a parent object that is a collection and it in turn is
> hosting 2 child objects.
[quoted text clipped - 96 lines]
>     }
> }

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.