serialize 2 objects to same xml file?

  • Thread starter Thread starter vince
  • Start date Start date
V

vince

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
 
Chad,

Thanks for the info, tried your method and got both
objects to serialize to the same file, and the xml looks
well-formed.

However, when attempting to deserialize the first object
in the file, I get an error, "There is an error in xml
document(7,1)"... Line 7 is the beginning of the second
serialized object.

In looking at a previous version of an xml file that
contained only the first object, the xml is identical for
that object in the new 2 object xml file...

It would seem that the Deserialize() method is choking on
the fact that there is more than one object to
deserialize...

Is there a step that I'm forgetting somewhere...??

thanks for any help...

vince
 
Actually, if you serialize 2 objects to XML, you'll
have two top-level tags which would be invalid XML.
I just thought of that, sorry.

You may have to have a root XML node and add the
serialized nodes underneath that.

What you may do is have something like:

<root>
<object num="1">
(serialization info here)
</object>
<object num="2">
(serialization info here)
</object>
</root>

Open the XML in an XmlDocument and do:

XmlNodeList objectNodes = doc.SelectNodes("//object");

Foo[] foos = new Foo[objectNodes.Count];

int fooCtr = 0;

foreach( XmlNode objectNode in objectNodes )
{
XmlSerializer ser = new XmlSerializer(typeof(Foo));

foos[fooCtr++] = (Foo) ser.Deserialize(
new StringReader(objectNode.InnerXml));
}

-c
 
Chad,

thanks again... so, to clarify, the workaround for the
problem of having two top level tags is to manually parse
the xml file that was created...???

Sorta new to xml so I'll have to take your code snippet
and study it for a bit...

Looks like you load an intermediate array with the two
chunks of object data that you've parsed outa the
original 2 object xml file... is this correct?

thanks for the help,

vince
-----Original Message-----
Actually, if you serialize 2 objects to XML, you'll
have two top-level tags which would be invalid XML.
I just thought of that, sorry.

You may have to have a root XML node and add the
serialized nodes underneath that.

What you may do is have something like:

<root>
<object num="1">
(serialization info here)
</object>
<object num="2">
(serialization info here)
</object>
</root>

Open the XML in an XmlDocument and do:

XmlNodeList objectNodes = doc.SelectNodes("//object");

Foo[] foos = new Foo[objectNodes.Count];

int fooCtr = 0;

foreach( XmlNode objectNode in objectNodes )
{
XmlSerializer ser = new XmlSerializer(typeof(Foo));

foos[fooCtr++] = (Foo) ser.Deserialize(
new StringReader(objectNode.InnerXml));
}

-c


vince said:
Chad,

Thanks for the info, tried your method and got both
objects to serialize to the same file, and the xml looks
well-formed.

However, when attempting to deserialize the first object
in the file, I get an error, "There is an error in xml
document(7,1)"... Line 7 is the beginning of the second
serialized object.

In looking at a previous version of an xml file that
contained only the first object, the xml is identical for
that object in the new 2 object xml file...

It would seem that the Deserialize() method is choking on
the fact that there is more than one object to
deserialize...

Is there a step that I'm forgetting somewhere...??

thanks for any help...

vince


.
 
vince said:
Chad,

thanks again... so, to clarify, the workaround for the
problem of having two top level tags is to manually parse
the xml file that was created...???

Kinda, I guess. XML must have ONE root tag.

You can have multiple tags underneath this, but the problem,
I think, is that the XmlSerializer won't understand this
extra tag. It expects a single tag with all the object
contents underneath.

So, if you wish to have multiple objects in a single
XML, you must accomodate the XmlSerializer.

To do this, I recommend adding another tag called
<object> or something and you can add your own meta-data
and attributes to this in the future (like the version of
the object contained herein, etc).

You must take the contents of that tag and pass it to
the XmlSerializer to deserialize.
Sorta new to xml so I'll have to take your code snippet
and study it for a bit...

Looks like you load an intermediate array with the two
chunks of object data that you've parsed outa the
original 2 object xml file... is this correct?

Since you have multiple objects in the XML to deserialize,
you have to store the newly reconstituted objects somewhere.
You can use an ArrayList, Hashtable, or most any Collection
for that matter.

I figured you wouldn't have that many objects and you
already know how many there are, so it's probably
most efficient just to create an array and add them as
you deserialize them from the XML

-c

-----Original Message-----
Actually, if you serialize 2 objects to XML, you'll
have two top-level tags which would be invalid XML.
I just thought of that, sorry.

You may have to have a root XML node and add the
serialized nodes underneath that.

What you may do is have something like:

<root>
<object num="1">
(serialization info here)
</object>
<object num="2">
(serialization info here)
</object>
</root>

Open the XML in an XmlDocument and do:

XmlNodeList objectNodes = doc.SelectNodes("//object");

Foo[] foos = new Foo[objectNodes.Count];

int fooCtr = 0;

foreach( XmlNode objectNode in objectNodes )
{
XmlSerializer ser = new XmlSerializer(typeof(Foo));

foos[fooCtr++] = (Foo) ser.Deserialize(
new StringReader(objectNode.InnerXml));
}

-c


vince said:
Chad,

Thanks for the info, tried your method and got both
objects to serialize to the same file, and the xml looks
well-formed.

However, when attempting to deserialize the first object
in the file, I get an error, "There is an error in xml
document(7,1)"... Line 7 is the beginning of the second
serialized object.

In looking at a previous version of an xml file that
contained only the first object, the xml is identical for
that object in the new 2 object xml file...

It would seem that the Deserialize() method is choking on
the fact that there is more than one object to
deserialize...

Is there a step that I'm forgetting somewhere...??

thanks for any help...

vince

-----Original Message-----

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...??

Create an XmlTextWriter (we'll call him xw)
Create Two XmlSerializers initialized with each type
and instance you wish to serialize. (we'll call them
1 and 2)

1.Serialize(xw);
2.Serialize(xw);

The only catch is, you have to deserialize them in
the same order, or remember how long 1 is. If you
wish to deserialize two, you have to create an
XmlTextReader and advance to the position in the
XML where 2 starts and then call:

2.Deserialize(xr);

-c


.


.
 
Chad,

so to "do it right" and have one root tag, I'd want to
create a serializable wrapper object that contained an
instance of each serializable object (2 in this case),
then serialize the wrapper object, correct?

I assume this would allow both contained objects to be
deserialized in the normal fashion...???

thanks for your time...

vince
-----Original Message-----

vince said:
Chad,

thanks again... so, to clarify, the workaround for the
problem of having two top level tags is to manually parse
the xml file that was created...???

Kinda, I guess. XML must have ONE root tag.

You can have multiple tags underneath this, but the problem,
I think, is that the XmlSerializer won't understand this
extra tag. It expects a single tag with all the object
contents underneath.

So, if you wish to have multiple objects in a single
XML, you must accomodate the XmlSerializer.

To do this, I recommend adding another tag called
<object> or something and you can add your own meta-data
and attributes to this in the future (like the version of
the object contained herein, etc).

You must take the contents of that tag and pass it to
the XmlSerializer to deserialize.
Sorta new to xml so I'll have to take your code snippet
and study it for a bit...

Looks like you load an intermediate array with the two
chunks of object data that you've parsed outa the
original 2 object xml file... is this correct?

Since you have multiple objects in the XML to deserialize,
you have to store the newly reconstituted objects somewhere.
You can use an ArrayList, Hashtable, or most any Collection
for that matter.

I figured you wouldn't have that many objects and you
already know how many there are, so it's probably
most efficient just to create an array and add them as
you deserialize them from the XML

-c

-----Original Message-----
Actually, if you serialize 2 objects to XML, you'll
have two top-level tags which would be invalid XML.
I just thought of that, sorry.

You may have to have a root XML node and add the
serialized nodes underneath that.

What you may do is have something like:

<root>
<object num="1">
(serialization info here)
</object>
<object num="2">
(serialization info here)
</object>
</root>

Open the XML in an XmlDocument and do:

XmlNodeList objectNodes = doc.SelectNodes("//object");

Foo[] foos = new Foo[objectNodes.Count];

int fooCtr = 0;

foreach( XmlNode objectNode in objectNodes )
{
XmlSerializer ser = new XmlSerializer(typeof (Foo));

foos[fooCtr++] = (Foo) ser.Deserialize(
new StringReader(objectNode.InnerXml));
}

-c


Chad,

Thanks for the info, tried your method and got both
objects to serialize to the same file, and the xml looks
well-formed.

However, when attempting to deserialize the first object
in the file, I get an error, "There is an error in xml
document(7,1)"... Line 7 is the beginning of the second
serialized object.

In looking at a previous version of an xml file that
contained only the first object, the xml is
identical
for
that object in the new 2 object xml file...

It would seem that the Deserialize() method is
choking
on
the fact that there is more than one object to
deserialize...

Is there a step that I'm forgetting somewhere...??

thanks for any help...

vince

-----Original Message-----

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...??

Create an XmlTextWriter (we'll call him xw)
Create Two XmlSerializers initialized with each type
and instance you wish to serialize. (we'll call them
1 and 2)

1.Serialize(xw);
2.Serialize(xw);

The only catch is, you have to deserialize them in
the same order, or remember how long 1 is. If you
wish to deserialize two, you have to create an
XmlTextReader and advance to the position in the
XML where 2 starts and then call:

2.Deserialize(xr);

-c


.



.


.
 
vince said:
Chad,

so to "do it right" and have one root tag, I'd want to
create a serializable wrapper object that contained an
instance of each serializable object (2 in this case),
then serialize the wrapper object, correct?

I assume this would allow both contained objects to be
deserialized in the normal fashion...???

That's not what I was saying, but that's an interesting
point. That might actually work. The only problem is,
if you wanted to change the number of subobjects, it
might be harder. You might just have a public propery
which is an array of the type of object you wish
to serialize.

As long as there's only one object, you don't need
the root tag (remember, you only need one top-level
tag, and if all you have is one object, then you
already have that).

I'm not sure which is the better approach. I haven't
had the chance to do a lot of serialization work
yet, so please try one or the other and let me know
how it goes.

-c
-----Original Message-----

vince said:
Chad,

thanks again... so, to clarify, the workaround for the
problem of having two top level tags is to manually parse
the xml file that was created...???

Kinda, I guess. XML must have ONE root tag.

You can have multiple tags underneath this, but the problem,
I think, is that the XmlSerializer won't understand this
extra tag. It expects a single tag with all the object
contents underneath.

So, if you wish to have multiple objects in a single
XML, you must accomodate the XmlSerializer.

To do this, I recommend adding another tag called
<object> or something and you can add your own meta-data
and attributes to this in the future (like the version of
the object contained herein, etc).

You must take the contents of that tag and pass it to
the XmlSerializer to deserialize.
Sorta new to xml so I'll have to take your code snippet
and study it for a bit...

Looks like you load an intermediate array with the two
chunks of object data that you've parsed outa the
original 2 object xml file... is this correct?

Since you have multiple objects in the XML to deserialize,
you have to store the newly reconstituted objects somewhere.
You can use an ArrayList, Hashtable, or most any Collection
for that matter.

I figured you wouldn't have that many objects and you
already know how many there are, so it's probably
most efficient just to create an array and add them as
you deserialize them from the XML

-c

-----Original Message-----
Actually, if you serialize 2 objects to XML, you'll
have two top-level tags which would be invalid XML.
I just thought of that, sorry.

You may have to have a root XML node and add the
serialized nodes underneath that.

What you may do is have something like:

<root>
<object num="1">
(serialization info here)
</object>
<object num="2">
(serialization info here)
</object>
</root>

Open the XML in an XmlDocument and do:

XmlNodeList objectNodes = doc.SelectNodes("//object");

Foo[] foos = new Foo[objectNodes.Count];

int fooCtr = 0;

foreach( XmlNode objectNode in objectNodes )
{
XmlSerializer ser = new XmlSerializer(typeof (Foo));

foos[fooCtr++] = (Foo) ser.Deserialize(
new StringReader(objectNode.InnerXml));
}

-c


Chad,

Thanks for the info, tried your method and got both
objects to serialize to the same file, and the xml
looks
well-formed.

However, when attempting to deserialize the first
object
in the file, I get an error, "There is an error in xml
document(7,1)"... Line 7 is the beginning of the second
serialized object.

In looking at a previous version of an xml file that
contained only the first object, the xml is identical
for
that object in the new 2 object xml file...

It would seem that the Deserialize() method is choking
on
the fact that there is more than one object to
deserialize...

Is there a step that I'm forgetting somewhere...??

thanks for any help...

vince

-----Original Message-----

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...??

Create an XmlTextWriter (we'll call him xw)
Create Two XmlSerializers initialized with each type
and instance you wish to serialize. (we'll call them
1 and 2)

1.Serialize(xw);
2.Serialize(xw);

The only catch is, you have to deserialize them in
the same order, or remember how long 1 is. If you
wish to deserialize two, you have to create an
XmlTextReader and advance to the position in the
XML where 2 starts and then call:

2.Deserialize(xr);

-c


.



.


.
 
Chad,

tried your method using an xmldoc, was able to do a
SelectNodes() on the first serialized object, but got
back a count of 0 when attempting to do the same thing
with the second serialized object...

I guess the extra root tag for the second object has got
me hosed everywhere, looks like I'll hafta look into
creating a wrapper for both objects...

thanks for your time...

vince
-----Original Message-----

vince said:
Chad,

so to "do it right" and have one root tag, I'd want to
create a serializable wrapper object that contained an
instance of each serializable object (2 in this case),
then serialize the wrapper object, correct?

I assume this would allow both contained objects to be
deserialized in the normal fashion...???

That's not what I was saying, but that's an interesting
point. That might actually work. The only problem is,
if you wanted to change the number of subobjects, it
might be harder. You might just have a public propery
which is an array of the type of object you wish
to serialize.

As long as there's only one object, you don't need
the root tag (remember, you only need one top-level
tag, and if all you have is one object, then you
already have that).

I'm not sure which is the better approach. I haven't
had the chance to do a lot of serialization work
yet, so please try one or the other and let me know
how it goes.

-c
-----Original Message-----

Chad,

thanks again... so, to clarify, the workaround for the
problem of having two top level tags is to manually parse
the xml file that was created...???

Kinda, I guess. XML must have ONE root tag.

You can have multiple tags underneath this, but the problem,
I think, is that the XmlSerializer won't understand this
extra tag. It expects a single tag with all the object
contents underneath.

So, if you wish to have multiple objects in a single
XML, you must accomodate the XmlSerializer.

To do this, I recommend adding another tag called
<object> or something and you can add your own meta- data
and attributes to this in the future (like the version of
the object contained herein, etc).

You must take the contents of that tag and pass it to
the XmlSerializer to deserialize.

Sorta new to xml so I'll have to take your code snippet
and study it for a bit...

Looks like you load an intermediate array with the two
chunks of object data that you've parsed outa the
original 2 object xml file... is this correct?

Since you have multiple objects in the XML to deserialize,
you have to store the newly reconstituted objects somewhere.
You can use an ArrayList, Hashtable, or most any Collection
for that matter.

I figured you wouldn't have that many objects and you
already know how many there are, so it's probably
most efficient just to create an array and add them as
you deserialize them from the XML

-c


-----Original Message-----
Actually, if you serialize 2 objects to XML, you'll
have two top-level tags which would be invalid XML.
I just thought of that, sorry.

You may have to have a root XML node and add the
serialized nodes underneath that.

What you may do is have something like:

<root>
<object num="1">
(serialization info here)
</object>
<object num="2">
(serialization info here)
</object>
</root>

Open the XML in an XmlDocument and do:

XmlNodeList objectNodes = doc.SelectNodes ("//object");

Foo[] foos = new Foo[objectNodes.Count];

int fooCtr = 0;

foreach( XmlNode objectNode in objectNodes )
{
XmlSerializer ser = new XmlSerializer(typeof (Foo));

foos[fooCtr++] = (Foo) ser.Deserialize(
new StringReader(objectNode.InnerXml));
}

-c


Chad,

Thanks for the info, tried your method and got both
objects to serialize to the same file, and the xml
looks
well-formed.

However, when attempting to deserialize the first
object
in the file, I get an error, "There is an error
in
xml
document(7,1)"... Line 7 is the beginning of the second
serialized object.

In looking at a previous version of an xml file that
contained only the first object, the xml is identical
for
that object in the new 2 object xml file...

It would seem that the Deserialize() method is choking
on
the fact that there is more than one object to
deserialize...

Is there a step that I'm forgetting somewhere...??

thanks for any help...

vince

-----Original Message-----

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...??

Create an XmlTextWriter (we'll call him xw)
Create Two XmlSerializers initialized with each type
and instance you wish to serialize. (we'll call them
1 and 2)

1.Serialize(xw);
2.Serialize(xw);

The only catch is, you have to deserialize them in
the same order, or remember how long 1 is. If you
wish to deserialize two, you have to create an
XmlTextReader and advance to the position in the
XML where 2 starts and then call:

2.Deserialize(xr);

-c


.



.



.


.
 
SelectNodes() takes an XPath query. This is kinda like
the directory structure in DOS/UNIX.

If you had, for example:

<root>
<object>...</object>
<object>...</object>
<object>...</object>
</root>

And you did a query on the doc like:

doc.SelectNodes("/root/object") or
even "//object", you would get back an
XmlNodeList with 3 nodes in it.

If you wanted to, you can call SelectNodes()
and/or SelectSingleNode() on an XmlNode as
well. It's useful if you're navigating a
huge tree of XML.

XPath is pretty cool, if you get some time,
you should read up on it.

-c

vince said:
Chad,

tried your method using an xmldoc, was able to do a
SelectNodes() on the first serialized object, but got
back a count of 0 when attempting to do the same thing
with the second serialized object...

I guess the extra root tag for the second object has got
me hosed everywhere, looks like I'll hafta look into
creating a wrapper for both objects...

thanks for your time...

vince
-----Original Message-----

vince said:
Chad,

so to "do it right" and have one root tag, I'd want to
create a serializable wrapper object that contained an
instance of each serializable object (2 in this case),
then serialize the wrapper object, correct?

I assume this would allow both contained objects to be
deserialized in the normal fashion...???

That's not what I was saying, but that's an interesting
point. That might actually work. The only problem is,
if you wanted to change the number of subobjects, it
might be harder. You might just have a public propery
which is an array of the type of object you wish
to serialize.

As long as there's only one object, you don't need
the root tag (remember, you only need one top-level
tag, and if all you have is one object, then you
already have that).

I'm not sure which is the better approach. I haven't
had the chance to do a lot of serialization work
yet, so please try one or the other and let me know
how it goes.

-c
-----Original Message-----

Chad,

thanks again... so, to clarify, the workaround for the
problem of having two top level tags is to manually
parse
the xml file that was created...???

Kinda, I guess. XML must have ONE root tag.

You can have multiple tags underneath this, but the
problem,
I think, is that the XmlSerializer won't understand this
extra tag. It expects a single tag with all the object
contents underneath.

So, if you wish to have multiple objects in a single
XML, you must accomodate the XmlSerializer.

To do this, I recommend adding another tag called
<object> or something and you can add your own meta- data
and attributes to this in the future (like the version of
the object contained herein, etc).

You must take the contents of that tag and pass it to
the XmlSerializer to deserialize.

Sorta new to xml so I'll have to take your code snippet
and study it for a bit...

Looks like you load an intermediate array with the two
chunks of object data that you've parsed outa the
original 2 object xml file... is this correct?

Since you have multiple objects in the XML to
deserialize,
you have to store the newly reconstituted objects
somewhere.
You can use an ArrayList, Hashtable, or most any
Collection
for that matter.

I figured you wouldn't have that many objects and you
already know how many there are, so it's probably
most efficient just to create an array and add them as
you deserialize them from the XML

-c


-----Original Message-----
Actually, if you serialize 2 objects to XML, you'll
have two top-level tags which would be invalid XML.
I just thought of that, sorry.

You may have to have a root XML node and add the
serialized nodes underneath that.

What you may do is have something like:

<root>
<object num="1">
(serialization info here)
</object>
<object num="2">
(serialization info here)
</object>
</root>

Open the XML in an XmlDocument and do:

XmlNodeList objectNodes = doc.SelectNodes ("//object");

Foo[] foos = new Foo[objectNodes.Count];

int fooCtr = 0;

foreach( XmlNode objectNode in objectNodes )
{
XmlSerializer ser = new XmlSerializer(typeof
(Foo));

foos[fooCtr++] = (Foo) ser.Deserialize(
new StringReader(objectNode.InnerXml));
}

-c


Chad,

Thanks for the info, tried your method and got both
objects to serialize to the same file, and the xml
looks
well-formed.

However, when attempting to deserialize the first
object
in the file, I get an error, "There is an error in
xml
document(7,1)"... Line 7 is the beginning of the
second
serialized object.

In looking at a previous version of an xml file that
contained only the first object, the xml is
identical
for
that object in the new 2 object xml file...

It would seem that the Deserialize() method is
choking
on
the fact that there is more than one object to
deserialize...

Is there a step that I'm forgetting somewhere...??

thanks for any help...

vince

-----Original Message-----

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...??

Create an XmlTextWriter (we'll call him xw)
Create Two XmlSerializers initialized with each
type
and instance you wish to serialize. (we'll call
them
1 and 2)

1.Serialize(xw);
2.Serialize(xw);

The only catch is, you have to deserialize them in
the same order, or remember how long 1 is. If you
wish to deserialize two, you have to create an
XmlTextReader and advance to the position in the
XML where 2 starts and then call:

2.Deserialize(xr);

-c


.



.



.


.
 
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
 
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
 
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








.
.
 
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:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndotnet/html/objserializ.asp
http://msdn.microsoft.com/library/default.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


.
.
 
Robert,

thanks for your help, it seems that my custom JobFile
type can't be serialized as a member of a strongly typed
array... tried several ways of doing this, but Serialize()
keeps telliing me that my JobFile type "can't be used in
this context".

I suspect that it's because the JobFile class inherits
from ICollection and is set up to serialize a contained
arraylist of custom objects. I think I'm trying to do too
much here, will look at your other suggested methods.

Here's the code I was using:

JobFile[] jobArray = new JobFile[2];

jobArray[0] = theJob;
jobArray[1] = theJob1;

XmlSerializer x = new XmlSerializer(jobArray.GetType());
TextWriter writer = new StreamWriter("c:\\backup\\" +
textBox1.Text + ".xml");
x.Serialize(writer, jobArray);

Thanks for your help,

Vince Lusardi
-----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


.

.


.
 
Back
Top