473,386 Members | 1,886 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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)xmlTextWrit er.BaseStream).ToArray()).Trim();
}
}

Console.WriteLine(xml);
}
}

Aug 22 '07 #1
2 3543
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?
Aug 22 '07 #2
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)xmlTextWrit er.BaseStream).ToArray()).Trim();
}
}

Console.WriteLine(xml);
}
}
"ilitirit" wrote:
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?

Aug 31 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Ivo Bronsveld | last post by:
All, I have quite a challenging task ahead of me. I need to write an object model (for code access) based on a schema, which cannot be made into a dataset because of it's complexity. So I...
2
by: Jasper Kent | last post by:
I'm trying to do the equivalent of using typedefs with templates in C++ to avoid long instantiation names. I can do this okay: using BigDictionary = System.Collections.Generic.Dictionary<int,...
3
by: RandomEngineer | last post by:
So here's the challenge... How can a collection (System.Collections.Generic.IList) of some custom type be serialized in a web service using .NET 2.0? Below are the class and the web methods in...
3
by: KH | last post by:
I can't seem to figure this one out... I've searched MSDN and Goog, and made my best guesses to no avail,, so help would be much appreciated! public ref class T sealed : public...
1
by: Peter Nofelt | last post by:
Hey All, I am having issue with serializing a class and its base class using ..net 2.0. I am using IXmlSerializable I've provided code displaying my at the bottom of this post. Thanks ahead...
0
by: Kurious Oranj | last post by:
This is the code that I currently have written to serialize the collection but it doesn't work when I put two "child" objects in. Anyone have any ideas what I'm doing wrong? ...
2
by: Fred Heida | last post by:
Hi, i'm trying to (using managed C++) implment the IEnumerable<Tinterface on my class.. but have a problem with the 2 GetEnumerator method required.... what i have done is... ...
5
by: Michi Henning | last post by:
I can pass a generic collection as ICollection<Tjust fine: static void flatCollection(ICollection<intc) {} // ... List<intl = new List<int>(); flatCollection(l); // Works fine Now I...
2
by: SimonDotException | last post by:
I am trying to use reflection in a property of a base type to inspect the properties of an instance of a type which is derived from that base type, when the properties can themselves be instances of...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.