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 / .NET Framework / XML / August 2007

Tip: Looking for answers? Try searching our database.

Serializing descendants of generic collections

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
ilitirit - 22 Aug 2007 14:53 GMT
Can anyone explain why the following program doesn't work?  The
attributes and elements of the MessageList class are not being
generated.

Am I doing something incorrectly?  Or if this is a bug in .NET, is
there a known workaround?

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

[Serializable, XmlRoot("Message")]
public class Message
{
   private int _id;
   private string _description;

   [XmlAttribute("id")]
   public int Id
   {
       get { return _id; }
       set { _id = value; }
   }

   [XmlElement("Description")]
   public string Description
   {
       get { return _description; }
       set { _description = value; }
   }
}

[Serializable, XmlRoot("MessageList")]
public class MessageList : List<Message>
{
   private int _id;
   private string _name = "";

   [XmlAttribute("id")]
   public int Id
   {
       get { return _id; }
       set { _id = value; }
   }

   [XmlElement("Name")]
   public string Name
   {
       get { return _name; }
       set { _name = value; }
   }
}

public class Program
{
   public static void Main()
   {
       Program test = new Program();

       Message m1 = new Message();
       m1.Id = 1;
       m1.Description = "Message 1";

       Message m2 = new Message();
       m2.Id = 2;
       m2.Description = "Message 2";

       MessageList messageList = new MessageList();

       messageList.Add(m1);
       messageList.Add(m2);

       messageList.Id = 20070822;

       test.Serialize(messageList);

       Console.ReadLine();
   }

   private void Serialize<T>(T obj)
   {
       string xml;
       XmlSerializer serializer = new XmlSerializer(typeof(T));

       using (MemoryStream memoryStream = new MemoryStream())
       {
           using (XmlTextWriter xmlTextWriter = new
XmlTextWriter(memoryStream, Encoding.UTF8))
           {
               xmlTextWriter.Formatting = Formatting.Indented;

               serializer.Serialize(xmlTextWriter, obj);
               xml =
Encoding.UTF8.GetString(((MemoryStream)xmlTextWriter.BaseStream).ToArray()).Trim();
           }
       }

       Console.WriteLine(xml);
   }
}
ilitirit - 22 Aug 2007 15:19 GMT
OK I found out that it's not really supported:
http://msdn2.microsoft.com/en-us/library/182eeyhh.aspx

Does anyone know of a suitable workaround?

> Can anyone explain why the following program doesn't work?  The
> attributes and elements of the MessageList class are not being
> generated.
>
> Am I doing something incorrectly?  Or if this is a bug in .NET, is
> there a known workaround?
p3john - 31 Aug 2007 15:14 GMT
Hi ilitirit, you could try this as a work-a-round:

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

[Serializable, XmlRoot("Message")]
public class Message
{
    private int _id;
    private string _description;

    [XmlAttribute("id")]
    public int Id
    {
        get { return _id; }
        set { _id = value; }
    }

    [XmlElement("Description")]
    public string Description
    {
        get { return _description; }
        set { _description = value; }
    }
}

[Serializable, XmlRoot("MessageList")]
public class MessageList : List<Message>, IXmlSerializable
{
    private int _id;
    private string _name = "";

    [XmlAttribute("id")]
    public int Id
    {
        get { return _id; }
        set { _id = value; }
    }

    [XmlElement("Name")]
    public string Name
    {
        get { return _name; }
        set { _name = value; }
    }

    #region IXmlSerializable Members

    public System.Xml.Schema.XmlSchema GetSchema()
    {
        throw new Exception("The method or operation is not implemented.");
    }

    public void ReadXml(XmlReader reader)
    {
        throw new Exception("The method or operation is not implemented.");
    }

    public void WriteXml(XmlWriter writer)
    {
        writer.WriteStartAttribute("MessageListId");
        writer.WriteValue(this.Id.ToString());
        writer.WriteEndAttribute();

        writer.WriteStartAttribute("MessageListName");
        writer.WriteValue(this.Name);
        writer.WriteEndAttribute();

        foreach (Message msg in this)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(Message));
            serializer.Serialize(writer, msg);
        }
    }

    #endregion
}

public class Program
{
    public static void Main()
    {
        Program test = new Program();

        Message m1 = new Message();
        m1.Id = 1;
        m1.Description = "Message 1";

        Message m2 = new Message();
        m2.Id = 2;
        m2.Description = "Message 2";

        MessageList messageList = new MessageList();

        messageList.Add(m1);
        messageList.Add(m2);

        messageList.Id = 20070822;

        test.Serialize(messageList);

        Console.ReadLine();
    }

    private void Serialize<T>(T obj)
    {
        string xml;
        XmlSerializer serializer = new XmlSerializer(typeof(T));

        using (MemoryStream memoryStream = new MemoryStream())
        {
            using (XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream,
Encoding.UTF8))
            {
                xmlTextWriter.Formatting = Formatting.Indented;

                serializer.Serialize(xmlTextWriter, obj);
                xml =
Encoding.UTF8.GetString(((MemoryStream)xmlTextWriter.BaseStream).ToArray()).Trim();
            }
        }

        Console.WriteLine(xml);
    }
}

> OK I found out that it's not really supported:
> http://msdn2.microsoft.com/en-us/library/182eeyhh.aspx
[quoted text clipped - 7 lines]
> > Am I doing something incorrectly?  Or if this is a bug in .NET, is
> > there a known workaround?

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.