470,631 Members | 1,622 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,631 developers. It's quick & easy.

Serializing Classes

Hi

I wrote a class, Document, which has an Array of ITask objects. In order to
serialize the tasks contained within the tasks array, I marked each task
class with an attribute [Serializable], and added an entry to the task
collection property (of Document).

[
XmlElement("foo", typeof(FooTask)),
XmlElement("baa", typeof(BaaTask)),
XmlElement("taa", typeof(TaaTask)),
]
public TaskCollection Tasks
{
get { return m_tasks; }
}

The number of tasks has increased so I'm looking at possibilities to improve
maintenace. Is there a way I can dynamically create the mapping of element
name and its type for the array list? I would like to have a configuration
file with such information and load it at run time, rather than hardcoding
the element names.

Thanks
Werner
Nov 11 '05 #1
2 2045
Sure ... you can "attach" XmlElement attributes programmatically by
populating an XmlOverrides object and passing it to the constructor of the
XmlSerializer.

I pasted an old example that I had below. You can set up your XmlOverrides
object with settings you read from the app.config file with a custom section
handler for example or with the brand new configuration appliacation block.

--
HTH
Christoph Schittko [MVP]
Software Architect, .NET Mentor
"Werner B. Strydom" <bl*******@hotmail.com> wrote in message
news:uV**************@TK2MSFTNGP11.phx.gbl...
Hi

I wrote a class, Document, which has an Array of ITask objects. In order to serialize the tasks contained within the tasks array, I marked each task
class with an attribute [Serializable], and added an entry to the task
collection property (of Document).

[
XmlElement("foo", typeof(FooTask)),
XmlElement("baa", typeof(BaaTask)),
XmlElement("taa", typeof(TaaTask)),
]
public TaskCollection Tasks
{
get { return m_tasks; }
}

The number of tasks has increased so I'm looking at possibilities to improve maintenace. Is there a way I can dynamically create the mapping of element
name and its type for the array list? I would like to have a configuration
file with such information and load it at run time, rather than hardcoding
the element names.

Thanks
Werner

Nov 11 '05 #2
Here's the promissed example:

using System;
using System.Xml;
using System.Xml.Serialization;
using System.Collections;

// NO XmlInclude attributes
// to declare derived types
public class Vehicle
{
public Vehicle(){}
public string Make;
public string Model;
public int Year;
}
public class Car : Vehicle
{
public Car() {}
public string VIN;
}
public class Motorcycle : Vehicle
{
public Motorcycle() {}
public bool VeryLoud;
}

public class ParkingLot
{
public ArrayList Cars;
}

public class AttributeOverrides
{
public static void Main()
{
ParkingLot lot = SetupParkingLot();
XmlTextWriter writer = new XmlTextWriter( Console.Out );
writer.Formatting = Formatting.Indented;

Console.WriteLine( "Writing Parking Lot Without Runtime Attributes:" );
SerializeParkingLot( writer, lot );
Console.WriteLine( "" );
writer.Close();
// need another writer because
// we can't write two more than one root element with the
// same XmlTextWriter instance
XmlTextWriter anotherWriter = new XmlTextWriter( Console.Out );
anotherWriter.Formatting = Formatting.Indented;
Console.WriteLine( "\n\nWriting Parking Lot With Runtime Attributes:" );
SerializeCustomParkingLot( anotherWriter, lot );
anotherWriter.Close();
}

private static ParkingLot SetupParkingLot()
{
ParkingLot lot = new ParkingLot();
lot.Cars = new ArrayList();
Car wifesCar = new Car();
wifesCar.Make = "Ford";
wifesCar.Model = "Explorer";
wifesCar.Year = 1997;
wifesCar.VIN = "ABC123DEF";

lot.Cars.Add( wifesCar );

return lot;
}

private static void SerializeParkingLot(XmlWriter writer,
ParkingLot parkingLot)
{
XmlSerializer xs = new XmlSerializer( typeof(ParkingLot),
new Type[] { typeof(Car) } );

xs.Serialize( writer, parkingLot );
}

private static void SerializeCustomParkingLot(XmlWriter writer,
ParkingLot parkingLot)
{
XmlAttributes carsAttributes = new XmlAttributes();
XmlAttributes classAttributes =
new XmlAttributes();

classAttributes.XmlRoot =
new XmlRootAttribute("ParkingLotRoot");

carsAttributes.XmlArrayItems.Add( new
XmlArrayItemAttribute("ParkedCar", typeof(Car)));

XmlAttributeOverrides overrides =
new XmlAttributeOverrides();

overrides.Add(typeof(ParkingLot), classAttributes);
overrides.Add(typeof(ParkingLot), "Cars", carsAttributes);

try
{
XmlSerializer xs = new XmlSerializer(
typeof(ParkingLot), overrides );

xs.Serialize( writer, parkingLot );
}
catch( InvalidOperationException )
{
System.Console.WriteLine( "Bad override attributes" );
}
}

}

and here's a link to the configuration management application block:

http://www.gotdotnet.com/Community/W...e-fa4bf2e3080f
--
HTH
Christoph Schittko [MVP]
Software Architect, .NET Mentor
"Werner B. Strydom" <bl*******@hotmail.com> wrote in message
news:uV**************@TK2MSFTNGP11.phx.gbl...
Hi

I wrote a class, Document, which has an Array of ITask objects. In order to serialize the tasks contained within the tasks array, I marked each task
class with an attribute [Serializable], and added an entry to the task
collection property (of Document).

[
XmlElement("foo", typeof(FooTask)),
XmlElement("baa", typeof(BaaTask)),
XmlElement("taa", typeof(TaaTask)),
]
public TaskCollection Tasks
{
get { return m_tasks; }
}

The number of tasks has increased so I'm looking at possibilities to improve maintenace. Is there a way I can dynamically create the mapping of element
name and its type for the array list? I would like to have a configuration
file with such information and load it at run time, rather than hardcoding
the element names.

Thanks
Werner

Nov 11 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Angelos Karantzalis | last post: by
2 posts views Thread by Aleksei Guzev | last post: by
reply views Thread by Ante Smolcic | last post: by
4 posts views Thread by Wayne Wengert | last post: by
1 post views Thread by Ivo Bronsveld | last post: by
10 posts views Thread by copx | last post: by
2 posts views Thread by Tobias Zimmergren | last post: by
8 posts views Thread by Joe | last post: by
2 posts views Thread by Simon | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.