DotNetNewsgroup.com  
web access to complete list of Microsoft.NET newsgroups
   home   |   control panel login   |   archive  |  
 
  carried group
academic
adonet
aspnet
aspnet.announcements
aspnet.buildingcontrols
aspnet.caching
aspnet.datagridcontrol
aspnet.mobile
aspnet.security
aspnet.webcontrols
aspnet.webservices
assignment_manager
datatools
dotnet.distributed_apps
dotnet.general
dotnet.myservices
dotnet.nternationalization
dotnet.scripting
dotnet.security
dotnet.vjsharp
dotnet.vsa
dotnet.xml
dotnetfaqs
framework
framework.clr
framework.compactframework
framework.component_services
framework.controls
framework.databinding
framework.drawing
framework.enhancements
framework.interop
framework.odbcnet
framework.performance
framework.remoting
framework.sdk
framework.setup
framework.webservices
framework.windowsforms
framework.wmi
frwk.windowsforms.designtime
lang.csharp
lang.jscript
lang.vb
lang.vb.controls
lang.vb.data
lang.vb.upgrade
lang.vc
lang.vc.libraries
  
 
start date: Wed, 22 Aug 2007 06:53:08 -0700,    posted on: microsoft.public.dotnet.xml        back       

Thread Index
  1    ilitirit
          2    ilitirit


Serializing descendants of generic collections   
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);
    }
}
Date:Wed, 22 Aug 2007 06:53:08 -0700   Author:  

Re: Serializing descendants of generic collections   
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?

ilitirit wrote:

> 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?
Date:Wed, 22 Aug 2007 07:19:52 -0700   Author:  

Google
 
Web dotnetnewsgroup.com


COPYRIGHT ?2005, EUROFRONT WORLDWIDE LTD., ALL RIGHT RESERVE  |   Contact us