-----Original Message-----
You're right -- it turns out you can't just stuff an Object[] array with
custom classes and expect it to serailize correctly. The reason is that the
XmlSerializer is kind of dumb... it needs to know what type of object is
being stored in the array so that it can deserialize it correctly later on.
Sorry for leading you astray. <g>
The easiest way around this is to use a strongly-typed array or collection.
So, it should work if you replace the line "object[] jobArray = new
object[2];" with "jobFile[] jobArray = new jobFile[2];" I have some sample
code below. Alternatively, you could define a strongly- typed JobFile
collection by inheriting from CollectionBase.
Another alternative is to use the SOAP serializer or binary serializer
instead. Both are more robust than the XmlSerializer, and should be able to
serialize Object[] arrays correctly. The tradeoff is that the output from
those serializers are not as easily readable (for human eyes) than the clean
XML generated by the XmlSerailizer. Check out these articles:
url=/library/en-us/dndotnet/html/objserializ.asp
url=/library/en-us/dnadvnet/html/vbnet09252001.asp
Hope this helps,
Robert Jacobson
using System;
using System.Xml.Serialization;
using System.IO;
using System.Diagnostics;
namespace ConsoleApplication1
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
SerializationTest();
}
static void SerializationTest()
{
Object[] jobArray = new Object[2];
DummyJob job1 = new DummyJob();
job1.Name = "First Job";
jobArray[0] = job1;
DummyJob job2 = new DummyJob();
job2.Name = "Second Job";
jobArray[1] = job2;
try
{
XmlSerializer serializer = new XmlSerializer (jobArray.GetType());
StreamWriter sw = new StreamWriter("Test.xml");
serializer.Serialize(sw, jobArray);
sw.Close();
}
catch (Exception ex)
{
Debug.WriteLine(ex.ToString());
}
}
}
public class DummyJob
{
public string Name;
}
}
vince said:
Robert,
I would also add that theJob seriailizes fine
individually, but I can't seem to get it to do so when
it's an element of a parent container...
thanks for your time...
vince
-----Original Message-----
Robert,
tried your suggestion of putting the 2 serializable
objects into an Arraylist, and also an array of type
object[], but I get runtime errors... here's the code:
object[] jobArray = new object[2];
jobArray[0] = theJob; //serializable custom type w/
arraylist
jobArray[1] = myTask; //arraylist
XmlSerializer x2 = new XmlSerializer(typeof(object []));
TextWriter tw = new StreamWriter("c:\\backup\\" +
textBox1.Text + ".xml");
x2.Serialize(tw, jobArray);
The exception thrown is "Job.JobFile may not be used in
this context".... Job is the namespace, and theJob is an
object of type Job.JobFile.
Did the same thing using an Arraylist to hold the two
objects with similar results...
Thanks for any further suggestions... sorry for the
double post on newsgroups..
-----Original Message-----
Vince,
I replied to your question in
microsoft.public.dotnet.general. In the
future, please don't repost the same question to
multiple groups.
There's no need to use strange workarounds to get this
done. Just define an
array of objects (or ArrayList, or strongly-typed
collection), place both
items in the array, and serialize/deserialize the
array. It will work fine.
--Robert Jacobson
Can I add (append) to an xml file that already
contains a
serialized object, and be able to deserialize to either
or both objects from the same file...??? How is this
done...??
thanks,
vince
.
.
.